HttpClient请求发送的几种用法:

 1  /// <summary>
 2         /// HttpClient实现Post请求
 3         /// </summary>
 4         static async void dooPost()
 5         {
 6             string url = "http://localhost:52824/api/register";
 7              //设置HttpClientHandler的AutomaticDecompression
 8             var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
 9             //创建HttpClient(注意传入HttpClientHandler)
10             using (var http = new HttpClient(handler))
11             {
12                 //使用FormUrlEncodedContent做HttpContent
13                 var content = new FormUrlEncodedContent(new Dictionary<string, string>()       
14                 {    {"Id","6"},
15                      {"Name","添加zzl"},
16                      {"Info", "添加动作"}//键名必须为空
17                  });
18 
19                 //await异步等待回应
20 
21                 var response = await http.PostAsync(url, content);
22                 //确保HTTP成功状态值
23                 response.EnsureSuccessStatusCode();
24                 //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
25                 Console.WriteLine(await response.Content.ReadAsStringAsync());
26             }
27 
28         }
29         /// <summary>
30         /// HttpClient实现Get请求
31         /// </summary>
32         static async void dooGet()
33         {
34             string url = "http://localhost:52824/api/register?id=1";
35             //创建HttpClient(注意传入HttpClientHandler)
36             var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
37 
38             using (var http = new HttpClient(handler))
39             {
40                 //await异步等待回应
41                 var response = await http.GetAsync(url);
42                 //确保HTTP成功状态值
43                 response.EnsureSuccessStatusCode();
44 
45                 //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
46                 Console.WriteLine(await response.Content.ReadAsStringAsync());
47             }
48         }
49         /// <summary>
50         /// HttpClient实现Put请求
51         /// </summary>
52         static async void dooPut()
53         {
54             var userId = 1;
55             string url = "http://localhost:52824/api/register?userid=" + userId;
56 
57             //设置HttpClientHandler的AutomaticDecompression
58             var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
59             //创建HttpClient(注意传入HttpClientHandler)
60             using (var http = new HttpClient(handler))
61             {
62                 //使用FormUrlEncodedContent做HttpContent
63                 var content = new FormUrlEncodedContent(new Dictionary<string, string>()       
64                 {
65                    {"Name","修改zzl"},
66                    {"Info", "Put修改动作"}//键名必须为空
67                 });
68 
69                 //await异步等待回应
70 
71                 var response = await http.PutAsync(url, content);
72                 //确保HTTP成功状态值
73                 response.EnsureSuccessStatusCode();
74                 //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
75                 Console.WriteLine(await response.Content.ReadAsStringAsync());
76             }
77 
78         }
 
   /// <summary>
        /// HttpClient异步请求
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        private static async Task<HttpResponseMessage> SendAsync(string uri, string content) 
        {
            HttpContent strContent = new StringContent(content);
            strContent.Headers.Clear();
            
            strContent.Headers.Add("Content-Type", "application/soap+xml;charset=utf-8");//"application/soap+xml; charset=utf-8"
            //strContent.Headers.Add("Content-Length", content.Length.ToString());
            HttpClient client = new HttpClient();
            HttpResponseMessage task=await client.PostAsync(uri, strContent);
            
            return task;
        }
//调用如下:
   static void Main(string[] args)
        {
            var soap = SOAPMessage("testcase009", "123456", "hello,【华信】", "", DateTime.Now.AddYears(-1));
            var rlt = SendAsync("http://192.168.1.22/webservice.asmx", soap);
            Console.WriteLine(rlt.Result.Content.ReadAsStringAsync().Result);
            Console.ReadKey();
        }
原文地址:https://www.cnblogs.com/rengke2002/p/6042790.html