You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
3.5 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net;
  4. using System.Net.Http;
  5. namespace EasyBL
  6. {
  7. public class WebApiUtil
  8. {
  9. /// <summary>
  10. /// HttpClient实现Post请求(异步)
  11. /// </summary>
  12. private static async void DooPost()
  13. {
  14. var url = "http://localhost:52824/api/register";
  15. //设置HttpClientHandler的AutomaticDecompression
  16. var handler = new HttpClientHandler() { AutomaticDecompression = DecompressionMethods.GZip };
  17. //创建HttpClient(注意传入HttpClientHandler)
  18. using (var http = new HttpClient(handler))
  19. {
  20. //使用FormUrlEncodedContent做HttpContent
  21. var content = new FormUrlEncodedContent(new Dictionary<string, string>
  22. { {"Id","6"},
  23. {"Name","添加zzl"},
  24. {"Info", "添加动作"}//键名必须为空
  25. });
  26. //await异步等待回应
  27. var response = await http.PostAsync(url, content);
  28. //确保HTTP成功状态值
  29. response.EnsureSuccessStatusCode();
  30. //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
  31. Console.WriteLine(await response.Content.ReadAsStringAsync());
  32. }
  33. }
  34. /// <summary>
  35. /// HttpClient实现Get请求(异步)
  36. /// </summary>
  37. private static async void DooGet()
  38. {
  39. var url = "http://localhost:52824/api/register?id=1";
  40. //创建HttpClient(注意传入HttpClientHandler)
  41. var handler = new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip };
  42. using (var http = new HttpClient(handler))
  43. {
  44. //await异步等待回应
  45. var response = await http.GetAsync(url);
  46. //确保HTTP成功状态值
  47. response.EnsureSuccessStatusCode();
  48. //await异步读取最后的JSON(注意此时gzip已经被自动解压缩了,因为上面的AutomaticDecompression = DecompressionMethods.GZip)
  49. Console.WriteLine(await response.Content.ReadAsStringAsync());
  50. }
  51. }
  52. /// <summary>
  53. /// HttpClient实现Put请求(异步)
  54. /// </summary>
  55. private static async void DooPut()
  56. {
  57. var userId = 1;
  58. var url = "http://localhost:52824/api/register?userid=" + userId;
  59. //设置HttpClientHandler的AutomaticDecompression
  60. var handler = new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip };
  61. //创建HttpClient(注意传入HttpClientHandler)
  62. using (var http = new HttpClient(handler))
  63. {
  64. //使用FormUrlEncodedContent做HttpContent
  65. var content = new FormUrlEncodedContent(new Dictionary<string, string>
  66. {
  67. {"Name","修改zzl"},
  68. {"Info", "Put修改动作"}//键名必须为空
  69. });
  70. //await异步等待回应
  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. }
  79. }