DefaultHttpClient.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using Newtonsoft.Json;
  2. using System.Collections.Concurrent;
  3. using System.Net;
  4. using System.Text;
  5. using AmrControl.Common.HttpClients;
  6. namespace AmrControl.Common.HttpClients
  7. {
  8. /// <summary>
  9. /// 请求客户端
  10. /// </summary>
  11. public class DefaultHttpClient
  12. {
  13. /// <summary>
  14. /// 用于加锁,防止对象重复构建
  15. /// </summary>
  16. private static object lockObj = new Object();
  17. /// <summary>
  18. /// httpClient,用于http访问
  19. /// </summary>
  20. private readonly HttpClient _httpClient;
  21. /// <summary>
  22. /// get客户端
  23. /// </summary>
  24. private readonly HttpGet httpGet;
  25. /// <summary>
  26. /// post客户端
  27. /// </summary>
  28. private readonly HttpPost httpPost;
  29. /// <summary>
  30. /// put操作客户端
  31. /// </summary>
  32. private readonly HttpPut httpPut;
  33. /// <summary>
  34. /// delete客户端
  35. /// </summary>
  36. private readonly HttpDelete httpDelete;
  37. /// <summary>
  38. /// 地址终端维护
  39. /// </summary>
  40. private static volatile ConcurrentDictionary<string, DefaultHttpClient> clients = new();
  41. //域名+地址,如http://127.0.0.1:5089/
  42. private readonly string baseUri;
  43. /// <summary>
  44. /// 私有化构造方法
  45. /// </summary>
  46. private DefaultHttpClient(string hostAddr)
  47. {
  48. var socketsHttpHandler = new SocketsHttpHandler()
  49. {
  50. //每个请求最大连接数
  51. MaxConnectionsPerServer = 128,
  52. //连接池中TCP连接最多可以闲置多久,设置180秒超时回收
  53. PooledConnectionIdleTimeout = TimeSpan.FromSeconds(180),
  54. //连接超时,默认60s
  55. ConnectTimeout = TimeSpan.FromSeconds(60),
  56. // http响应头最大字节数(单位:KB)
  57. MaxResponseHeadersLength = 128,
  58. //启用压缩
  59. AutomaticDecompression = DecompressionMethods.GZip,
  60. };
  61. _httpClient = new(socketsHttpHandler)
  62. {
  63. //等待响应超时时间,默认:100秒。
  64. Timeout = TimeSpan.FromSeconds(60)
  65. };
  66. this.baseUri = hostAddr;
  67. this.httpGet = new HttpGet(_httpClient, hostAddr);
  68. this.httpPost = new HttpPost(_httpClient, hostAddr);
  69. this.httpPut = new HttpPut(_httpClient, hostAddr);
  70. this.httpDelete = new HttpDelete(_httpClient, hostAddr);
  71. }
  72. /// <summary>
  73. /// 获取httpGet客户端
  74. /// </summary>
  75. /// <returns></returns>
  76. public HttpGet httpGetClient()
  77. {
  78. return httpGet;
  79. }
  80. /// <summary>
  81. /// 获取httpPost客户端
  82. /// </summary>
  83. /// <returns></returns>
  84. public HttpPost httpPostClient()
  85. {
  86. return httpPost;
  87. }
  88. /// <summary>
  89. /// 获取httpPut客户端
  90. /// </summary>
  91. /// <returns></returns>
  92. public HttpPut httpPutClient()
  93. {
  94. return httpPut;
  95. }
  96. /// <summary>
  97. /// 获取httpDelete客户端
  98. /// </summary>
  99. /// <returns></returns>
  100. public HttpDelete httpDeleteClient()
  101. {
  102. return httpDelete;
  103. }
  104. /// <summary>
  105. /// 获取客户端
  106. /// </summary>
  107. /// <returns></returns>
  108. public static DefaultHttpClient getClient(string baseUri)
  109. {
  110. if (!clients.ContainsKey(baseUri) || clients[baseUri] == null)
  111. {
  112. //尝试获取锁
  113. lock (lockObj)
  114. {
  115. //再次校验,防止重复构建
  116. if (!clients.ContainsKey(baseUri) || clients[baseUri] == null)
  117. {
  118. clients[baseUri] = new DefaultHttpClient(baseUri);
  119. }
  120. }
  121. }
  122. return clients[baseUri];
  123. }
  124. }
  125. }