HttpHelper.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Net.Http;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace TEAMModelOS.SDK.Helper.Network.HttpHelper
  7. {
  8. /// <summary>
  9. /// 需要调整的 https://blog.yowko.com/httpclientfactory-dotnet-core-dotnet-framework/
  10. /// </summary>
  11. public class HttpHelper
  12. {
  13. /// <summary>
  14. /// 同步GET请求
  15. /// </summary>
  16. /// <param name="url"></param>
  17. /// <param name="headers"></param>
  18. /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
  19. /// <returns></returns>
  20. public static string HttpGet(string url, Dictionary<string, string> headers = null, int timeout = 0)
  21. {
  22. using (HttpClient client = new HttpClient())
  23. {
  24. if (headers != null)
  25. {
  26. foreach (KeyValuePair<string, string> header in headers)
  27. {
  28. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  29. }
  30. }
  31. if (timeout > 0)
  32. {
  33. client.Timeout = new TimeSpan(0, 0, timeout);
  34. }
  35. Byte[] resultBytes = client.GetByteArrayAsync(url).Result;
  36. return Encoding.UTF8.GetString(resultBytes);
  37. }
  38. }
  39. /// <summary>
  40. /// 异步GET请求
  41. /// </summary>
  42. /// <param name="url"></param>
  43. /// <param name="headers"></param>
  44. /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
  45. /// <returns></returns>
  46. public static async Task<string> HttpGetAsync(string url, Dictionary<string, string> headers = null, int timeout = 0)
  47. {
  48. using (HttpClient client = new HttpClient())
  49. {
  50. if (headers != null)
  51. {
  52. foreach (KeyValuePair<string, string> header in headers)
  53. {
  54. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  55. }
  56. }
  57. if (timeout > 0)
  58. {
  59. client.Timeout = new TimeSpan(0, 0, timeout);
  60. }
  61. Byte[] resultBytes = await client.GetByteArrayAsync(url);
  62. return Encoding.Default.GetString(resultBytes);
  63. }
  64. }
  65. /// <summary>
  66. /// 同步POST请求
  67. /// </summary>
  68. /// <param name="url"></param>
  69. /// <param name="postData"></param>
  70. /// <param name="headers"></param>
  71. /// <param name="contentType"></param>
  72. /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
  73. /// <param name="encoding">默认UTF8</param>
  74. /// <returns></returns>
  75. public static string HttpPost(string url, string postData, Dictionary<string, string> headers = null, string contentType = null, int timeout = 0, Encoding encoding = null)
  76. {
  77. using (HttpClient client = new HttpClient())
  78. {
  79. if (headers != null)
  80. {
  81. foreach (KeyValuePair<string, string> header in headers)
  82. {
  83. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  84. }
  85. }
  86. if (timeout > 0)
  87. {
  88. client.Timeout = new TimeSpan(0, 0, timeout);
  89. }
  90. using (HttpContent content = new StringContent(postData ?? "", encoding ?? Encoding.UTF8))
  91. {
  92. if (contentType != null)
  93. {
  94. content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
  95. }
  96. using (HttpResponseMessage responseMessage = client.PostAsync(url, content).Result)
  97. {
  98. Byte[] resultBytes = responseMessage.Content.ReadAsByteArrayAsync().Result;
  99. return Encoding.UTF8.GetString(resultBytes);
  100. }
  101. }
  102. }
  103. }
  104. /// <summary>
  105. /// 异步POST请求
  106. /// </summary>
  107. /// <param name="url"></param>
  108. /// <param name="postData"></param>
  109. /// <param name="headers"></param>
  110. /// <param name="contentType"></param>
  111. /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
  112. /// <param name="encoding">默认UTF8</param>
  113. /// <returns></returns>
  114. public static async Task<string> HttpPostAsync(string url, string postData, Dictionary<string, string> headers = null, string contentType = null, int timeout = 0, Encoding encoding = null)
  115. {
  116. using (HttpClient client = new HttpClient())
  117. {
  118. if (headers != null)
  119. {
  120. foreach (KeyValuePair<string, string> header in headers)
  121. {
  122. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  123. }
  124. }
  125. if (timeout > 0)
  126. {
  127. client.Timeout = new TimeSpan(0, 0, timeout);
  128. }
  129. using (HttpContent content = new StringContent(postData ?? "", encoding ?? Encoding.UTF8))
  130. {
  131. if (contentType != null)
  132. {
  133. content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
  134. }
  135. using (HttpResponseMessage responseMessage = await client.PostAsync(url, content))
  136. {
  137. Byte[] resultBytes = await responseMessage.Content.ReadAsByteArrayAsync();
  138. return Encoding.UTF8.GetString(resultBytes);
  139. }
  140. }
  141. }
  142. }
  143. /// <summary>
  144. /// 异步POST请求
  145. /// </summary>
  146. /// <param name="url"></param>
  147. /// <param name="postData"></param>
  148. /// <param name="headers"></param>
  149. /// <param name="contentType"></param>
  150. /// <param name="timeout">请求响应超时时间,单位/s(默认100秒)</param>
  151. /// <param name="encoding">默认UTF8</param>
  152. /// <returns></returns>
  153. public static async Task<string> HttpPostAsync(string url, List<KeyValuePair<string, string>> postData, Dictionary<string, string> headers = null, string contentType = null, int timeout = 0, Encoding encoding = null)
  154. {
  155. using (HttpClient client = new HttpClient())
  156. {
  157. if (headers != null)
  158. {
  159. foreach (KeyValuePair<string, string> header in headers)
  160. {
  161. client.DefaultRequestHeaders.Add(header.Key, header.Value);
  162. }
  163. }
  164. if (timeout > 0)
  165. {
  166. client.Timeout = new TimeSpan(0, 0, timeout);
  167. }
  168. using (HttpContent content = new FormUrlEncodedContent(postData))
  169. {
  170. if (contentType != null)
  171. {
  172. content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);
  173. }
  174. using (HttpResponseMessage responseMessage = await client.PostAsync(url, content))
  175. {
  176. Byte[] resultBytes = await responseMessage.Content.ReadAsByteArrayAsync();
  177. return Encoding.UTF8.GetString(resultBytes);
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }