HttpClientSendCloud.cs 7.0 KB

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