Skip to content

Commit

Permalink
Merge pull request #32 from weiweiisachi/master
Browse files Browse the repository at this point in the history
httpClient更改为单例模式,增加httpClient连接池配置
  • Loading branch information
yd-dev authored Nov 19, 2024
2 parents da11e1b + 46fa835 commit f4b1796
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,28 @@ namespace Com.Netease.Is.Antispam.Demo
{
class Utils
{
// 单例 HttpClient 实例
private static readonly HttpClient HttpClientInstance;

static Utils()
{
// 配置 SocketsHttpHandler 参数
SocketsHttpHandler httpHandler = new SocketsHttpHandler
{
PooledConnectionLifetime = TimeSpan.FromMinutes(10), // 池中的连接最长存活时间
PooledConnectionIdleTimeout = TimeSpan.FromMinutes(2), // 池中的连接最大空闲时间
MaxConnectionsPerServer = 256 // 每个服务器的最大连接数
};

// 创建单例 HttpClient 实例,并配置默认请求头
HttpClientInstance = new HttpClient(httpHandler)
{
Timeout = TimeSpan.FromMilliseconds(10000) // 设置请求超时时间
};
HttpClientInstance.DefaultRequestHeaders.Connection.Add("keep-alive");
}


// 根据secretKey和parameters生成签名
public static String genSignature(String secretKey, Dictionary<String, String> parameters)
{
Expand Down Expand Up @@ -60,14 +82,12 @@ public static String genSignature(String secretKey, String signatureMethod, Dict

public static HttpClient makeHttpClient()
{
HttpClient client = new HttpClient() {};
client.DefaultRequestHeaders.Connection.Add("keep-alive");
client.SendAsync(new HttpRequestMessage
HttpClientInstance.SendAsync(new HttpRequestMessage
{
Method = new HttpMethod("HEAD"),
RequestUri = new Uri("http://as.dun.163.com")
}).Wait();
return client;
return HttpClientInstance;
}

// 执行post操作
Expand Down

0 comments on commit f4b1796

Please sign in to comment.