HttpHelper.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net.Http;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Infrastructure
  7. {
  8. public class HttpHelper
  9. {
  10. /// <summary>
  11. /// 发起POST同步请求
  12. /// </summary>
  13. /// <param name="url"></param>
  14. /// <param name="postData"></param>
  15. /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
  16. /// <param name="headers">填充消息头</param>
  17. /// <returns></returns>
  18. public static string HttpPost(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
  19. {
  20. Console.WriteLine($"【{DateTime.Now}】Post请求{url}");
  21. postData ??= "";
  22. using HttpClient client = new HttpClient();
  23. client.Timeout = new TimeSpan(0, 0, timeOut);
  24. if (headers != null)
  25. {
  26. foreach (var header in headers)
  27. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  28. }
  29. using HttpContent httpContent = new StringContent(postData, Encoding.UTF8);
  30. if (contentType != null)
  31. httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
  32. HttpResponseMessage response = client.PostAsync(url, httpContent).Result;
  33. return response.Content.ReadAsStringAsync().Result;
  34. }
  35. /// <summary>
  36. /// 发起POST异步请求
  37. /// </summary>
  38. /// <param name="url"></param>
  39. /// <param name="postData"></param>
  40. /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
  41. /// <param name="headers">填充消息头</param>
  42. /// <returns></returns>
  43. public static async Task<string> HttpPostAsync(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
  44. {
  45. postData ??= "";
  46. using HttpClient client = new HttpClient();
  47. client.Timeout = new TimeSpan(0, 0, timeOut);
  48. if (headers != null)
  49. {
  50. foreach (var header in headers)
  51. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  52. }
  53. using HttpContent httpContent = new StringContent(postData, Encoding.UTF8);
  54. if (contentType != null)
  55. httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
  56. HttpResponseMessage response = await client.PostAsync(url, httpContent);
  57. return await response.Content.ReadAsStringAsync();
  58. }
  59. /// <summary>
  60. /// 发起GET同步请求
  61. /// </summary>
  62. /// <param name="url"></param>
  63. /// <param name="headers"></param>
  64. /// <returns></returns>
  65. public static string HttpGet(string url, Dictionary<string, string> headers = null)
  66. {
  67. Console.WriteLine($"【{DateTime.Now}】Get请求{url}");
  68. using HttpClient client = new HttpClient();
  69. if (headers != null)
  70. {
  71. foreach (var header in headers)
  72. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  73. }
  74. else
  75. {
  76. client.DefaultRequestHeaders.Add("ContentType", "application/x-www-form-urlencoded");
  77. client.DefaultRequestHeaders.Add("UserAgent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
  78. }
  79. try
  80. {
  81. HttpResponseMessage response = client.GetAsync(url).Result;
  82. return response.Content.ReadAsStringAsync().Result;
  83. }
  84. catch (Exception ex)
  85. {
  86. //TODO 打印日志
  87. Console.WriteLine($"[Http请求出错]{url}|{ex.Message}");
  88. }
  89. return "";
  90. }
  91. /// <summary>
  92. /// 发起GET异步请求
  93. /// </summary>
  94. /// <param name="url"></param>
  95. /// <param name="headers"></param>
  96. /// <returns></returns>
  97. public static async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null)
  98. {
  99. using HttpClient client = new HttpClient();
  100. if (headers != null)
  101. {
  102. foreach (var header in headers)
  103. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  104. }
  105. HttpResponseMessage response = await client.GetAsync(url);
  106. return await response.Content.ReadAsStringAsync();
  107. }
  108. /// <summary>
  109. /// 发起Put同步请求
  110. /// </summary>
  111. /// <param name="url"></param>
  112. /// <param name="postData"></param>
  113. /// <param name="contentType">application/xml、application/json、application/text、application/x-www-form-urlencoded</param>
  114. /// <param name="headers">填充消息头</param>
  115. /// <returns></returns>
  116. public static string HttpPut(string url, string postData = null, string contentType = null, int timeOut = 30, Dictionary<string, string> headers = null)
  117. {
  118. postData ??= "";
  119. using HttpClient client = new HttpClient();
  120. client.Timeout = new TimeSpan(0, 0, timeOut);
  121. if (headers != null)
  122. {
  123. foreach (var header in headers)
  124. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  125. }
  126. using HttpContent httpContent = new StringContent(postData, Encoding.UTF8);
  127. if (contentType != null)
  128. httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
  129. HttpResponseMessage response = client.PutAsync(url, httpContent).Result;
  130. return response.Content.ReadAsStringAsync().Result;
  131. }
  132. }
  133. }