多线程之HttpClient

  在程序用调用 Http 接口、请求 http 资源、编写 http 爬虫等的时候都需要在程序集中进行 Http 请求。

  很多人习惯的 WebClient、HttpWebRequest 在 TPL 下很多用起来不方便的地方,TPL 下推荐使用 HttpClient(using System.Net.Http;),.net core 下已经不支持 WebClient 等

  HttpClient 发 出 Get 请 求 获 取 文 本 响 应 : string html = await hc.GetStringAsync("http://www.rupeng.com");

   HttpClient 发出Post请求使用

1 //第一个参数是请求的地址,第二个参数就是用来设置请求内容的
2 Task<HttpResponseMessage> PostAsync(string requestUri, HttpContent content)

  HttpContent是一个抽象类:主要的子类有:

    1.    FormUrlEncodedContent(表单格式请求)、
    2.   StringContent(字符串请求)、
    3.   MultipartFormDataContent(Multipart 表单请求,一般带上传文件信息)、
    4.   StreamContent(流内容)。

发送一个Get请求

1 private async void button1_Click(object sender, EventArgs e)
2         {
3             //发送异步的Get请求
4             HttpClient hc = new HttpClient();
5             //await hc.GetByteArrayAsync()
6             //await hc.GetStreamAsync()
7             string html=await hc.GetStringAsync("http://127.0.0.1:8081/Home/Login");
8             textBox1.Text = html;
9         }

发送一个POST表单格式请求:

 1 private async void button2_Click(object sender, EventArgs e)
 2         {
 3             HttpClient hc = new HttpClient();
 4             //List<KeyValuePair<string, string>> nameValues = new List<KeyValuePair<string, string>>();
 5             //nameValues.Add(new KeyValuePair<string, string>("userName", "admin123"));
 6             //nameValues.Add(new KeyValuePair<string, string>("password", "123123123"));
 7             //Dictionary也是实现了IEnumerable接口的,所以也可以这样传值
 8             Dictionary<string, string> nameValues = new Dictionary<string, string>();
 9             nameValues["userName"] = "admin123";
10             nameValues["password"] = "123123123";
11             FormUrlEncodedContent content = new FormUrlEncodedContent(nameValues);
12             HttpResponseMessage msg= await hc.PostAsync("http://127.0.0.1:8081/Home/Login", content);
13             //msg.Content;返回的Http报问题
14             //msg.Headers;获得响应头
15             //msg.StatusCode;获得响应的状态码
16 
17         }

发送一个POST字符串请求:

 1 private async void button3_Click(object sender, EventArgs e)
 2         {
 3             string json = "{userName:'admin',password:'123'}";
 4             HttpClient client = new HttpClient();
 5             StringContent content = new StringContent(json);
 6             //contentype 设置协议类型,必不可少
 7             content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
 8             var respMsg = await client.PostAsync("http://127.0.0.1:6666/Home/Login2/", content);
 9             string msgBody = await respMsg.Content.ReadAsStringAsync();
10         }

发送一个POST,Multipart 表单上传请求:

 1 private async void button4_Click(object sender, EventArgs e)
 2         {
 3             HttpClient client = new HttpClient();
 4             MultipartFormDataContent content = new MultipartFormDataContent();
 5             content.Headers.Add("UserName", "admin");
 6             content.Headers.Add("Password", "123");
 7             //先读取文件
 8             using (Stream stream = File.OpenRead(@"D:	empxx.png"))
 9             {
10                 //                       放入流中     上传接口协议名file  文件名
11                 content.Add(new StreamContent(stream), "file", "logo.png");
12                 var respMsg = await client.PostAsync("http://127.0.0.1:6666/Home/Upload/", content);
13                 string msgBody = await respMsg.Content.ReadAsStringAsync();
14                 MessageBox.Show(respMsg.StatusCode.ToString());
15                 MessageBox.Show(msgBody);
16             }
17         }
View Code

 

原文地址:https://www.cnblogs.com/cuijl/p/7656371.html