HttpClientService.cs 6.7 KB

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