123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using Newtonsoft.Json;
- using System.Collections.Concurrent;
- using System.Net;
- using System.Text;
- using AmrControl.Common.HttpClients;
- namespace AmrControl.Common.HttpClients
- {
- /// <summary>
- /// 请求客户端
- /// </summary>
- public class DefaultHttpClient
- {
- /// <summary>
- /// 用于加锁,防止对象重复构建
- /// </summary>
- private static object lockObj = new Object();
- /// <summary>
- /// httpClient,用于http访问
- /// </summary>
- private readonly HttpClient _httpClient;
- /// <summary>
- /// get客户端
- /// </summary>
- private readonly HttpGet httpGet;
- /// <summary>
- /// post客户端
- /// </summary>
- private readonly HttpPost httpPost;
- /// <summary>
- /// put操作客户端
- /// </summary>
- private readonly HttpPut httpPut;
- /// <summary>
- /// delete客户端
- /// </summary>
- private readonly HttpDelete httpDelete;
- /// <summary>
- /// 地址终端维护
- /// </summary>
- private static volatile ConcurrentDictionary<string, DefaultHttpClient> clients = new();
- //域名+地址,如http://127.0.0.1:5089/
- private readonly string baseUri;
- /// <summary>
- /// 私有化构造方法
- /// </summary>
- private DefaultHttpClient(string hostAddr)
- {
- var socketsHttpHandler = new SocketsHttpHandler()
- {
- //每个请求最大连接数
- MaxConnectionsPerServer = 128,
- //连接池中TCP连接最多可以闲置多久,设置180秒超时回收
- PooledConnectionIdleTimeout = TimeSpan.FromSeconds(180),
- //连接超时,默认60s
- ConnectTimeout = TimeSpan.FromSeconds(60),
- // http响应头最大字节数(单位:KB)
- MaxResponseHeadersLength = 128,
- //启用压缩
- AutomaticDecompression = DecompressionMethods.GZip,
- };
- _httpClient = new(socketsHttpHandler)
- {
- //等待响应超时时间,默认:100秒。
- Timeout = TimeSpan.FromSeconds(60)
- };
- this.baseUri = hostAddr;
- this.httpGet = new HttpGet(_httpClient, hostAddr);
- this.httpPost = new HttpPost(_httpClient, hostAddr);
- this.httpPut = new HttpPut(_httpClient, hostAddr);
- this.httpDelete = new HttpDelete(_httpClient, hostAddr);
- }
- /// <summary>
- /// 获取httpGet客户端
- /// </summary>
- /// <returns></returns>
- public HttpGet httpGetClient()
- {
- return httpGet;
- }
- /// <summary>
- /// 获取httpPost客户端
- /// </summary>
- /// <returns></returns>
- public HttpPost httpPostClient()
- {
- return httpPost;
- }
- /// <summary>
- /// 获取httpPut客户端
- /// </summary>
- /// <returns></returns>
- public HttpPut httpPutClient()
- {
- return httpPut;
- }
- /// <summary>
- /// 获取httpDelete客户端
- /// </summary>
- /// <returns></returns>
- public HttpDelete httpDeleteClient()
- {
- return httpDelete;
- }
- /// <summary>
- /// 获取客户端
- /// </summary>
- /// <returns></returns>
- public static DefaultHttpClient getClient(string baseUri)
- {
- if (!clients.ContainsKey(baseUri) || clients[baseUri] == null)
- {
- //尝试获取锁
- lock (lockObj)
- {
- //再次校验,防止重复构建
- if (!clients.ContainsKey(baseUri) || clients[baseUri] == null)
- {
- clients[baseUri] = new DefaultHttpClient(baseUri);
- }
- }
- }
- return clients[baseUri];
- }
- }
- }
|