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();
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM