HttpClient测试

tech2022-12-09  53

HttpClient测试

using System.Collections.Generic; using System.IO; using System.Net.Http; using System.Net.Http.Headers; using System.Windows; namespace WpfHttpClient { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { private readonly IHttpClientFactory _httpClientFactory; HttpClient client; public MainWindow(IHttpClientFactory httpClientFactory) { InitializeComponent(); _httpClientFactory = httpClientFactory; client = _httpClientFactory.CreateClient(); } private async void ButtonGET_Click(object sender, RoutedEventArgs e) { //发送异步的Get请求 string html_get_content = await client.GetStringAsync("http://httpbin.org/get"); ShowInfo(html_get_content); } private async void ButtonPost1_Click(object sender, RoutedEventArgs e) { MultipartFormDataContent content = new MultipartFormDataContent(); content.Headers.Add("UserName", "admin"); content.Headers.Add("Password", "123"); //先读取文件 using (Stream stream = File.OpenRead(@"oldMan.jpg")) { // 放入流中 上传接口协议名file 文件名 content.Add(new StreamContent(stream), "oldMan", "oldMan.jpg"); var respMsg = await client.PostAsync("http://httpbin.org/post", content); string html_post_content = await respMsg.Content.ReadAsStringAsync(); ShowInfo(html_post_content); } } private async void ButtonPost2_Click(object sender, RoutedEventArgs e) { string json = "{userName:'admin',password:'123'}"; StringContent content = new StringContent(json); //contentype 设置协议类型,必不可少 content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); var respMsg = await client.PostAsync("http://httpbin.org/post", content); string html_post_content = await respMsg.Content.ReadAsStringAsync(); ShowInfo(html_post_content); } private async void ButtonPost3_Click(object sender, RoutedEventArgs e) {//POST表单格式 Dictionary<string, string> nameValues = new Dictionary<string, string>(); nameValues["userName"] = "admin"; nameValues["password"] = "8888888"; FormUrlEncodedContent content = new FormUrlEncodedContent(nameValues); //contentype 设置协议类型,必不可少 content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); var respMsg = await client.PostAsync("http://httpbin.org/post", content); string html_post_content = await respMsg.Content.ReadAsStringAsync(); ShowInfo(html_post_content); } private void ShowInfo(string msg) { this.txtInfo.Text = msg; } } } /*回显请求中用于以下任何类型的数据 http://httpbin.org/ip返回原始IP。 http://httpbin.org/user-agent返回用户代理。 http://httpbin.org/headers返回标头字典。 http://httpbin.org/get返回GET数据。 http://httpbin.org/post返回POST数据。 http://httpbin.org/put返回PUT数据。 http://httpbin.org/delete返回DELETE数据 http://httpbin.org/gzip返回gzip编码的数据。 http://httpbin.org/status/:code返回给定的HTTP状态代码。 http://httpbin.org/response-headers?key=val返回给定的响应头。 http://httpbin.org/redirect/:n 302重定向n次。 http://httpbin.org/relative-redirect/:n 302相对重定向n次。 http://httpbin.org/cookies返回cookie数据。 http://httpbin.org/cookies/set/:name/:value设置一个简单的cookie。 http://httpbin.org/basic-auth/:user/:passwd挑战HTTP基本认证。 http://httpbin.org/hidden-basic-auth/:user/:passwd 404的BasicAuth。 http://httpbin.org/digest-auth/:qop/:user/:passwd挑战HTTP摘要验证。 http://httpbin.org/stream/:n流n-100行。 http://httpbin.org/delay/:n延迟响应n-10秒。*/
最新回复(0)