HttpClientUserInfo.cs 6.9 KB

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