Skip to content

Commit 7df60d2

Browse files
committed
feature(static net): let it use http client pool ( except has token)
1 parent 850f911 commit 7df60d2

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

PCL.Neo.Core/Utils/Net/StaticNet.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ namespace PCL.Neo.Core.Utils.Net;
99
/// </summary>
1010
public static class StaticNet
1111
{
12+
private static readonly string SharedName;
13+
14+
static StaticNet()
15+
{
16+
var sharedName = Guid.NewGuid().ToString()[..8];
17+
var clientInfo = HttpClientPool.AddHttpClient(sharedName, new HttpClient(), TimeSpan.FromMinutes(5));
18+
clientInfo.InUse = false;
19+
SharedName = sharedName;
20+
}
21+
1222
/// <summary>
1323
/// 发送无内容信息
1424
/// </summary>
@@ -23,7 +33,7 @@ public static async Task<HttpResponseMessage> SendAsync(HttpMethod httpMethod, U
2333

2434
try
2535
{
26-
var httpClient = new HttpClient();
36+
var httpClient = GetHttpClient(ref token);
2737
if (!string.IsNullOrEmpty(token))
2838
{
2939
httpClient.DefaultRequestHeaders.Authorization =
@@ -57,7 +67,7 @@ public static async Task<HttpResponseMessage> SendAsync(HttpRequestMessage reque
5767

5868
try
5969
{
60-
var httpClient = new HttpClient();
70+
var httpClient = GetHttpClient(ref token);
6171
if (!string.IsNullOrEmpty(token))
6272
{
6373
httpClient.DefaultRequestHeaders.Authorization =
@@ -91,7 +101,7 @@ public static async Task<T> SendAsync<T>(HttpMethod httpMethod, Uri url, string?
91101

92102
try
93103
{
94-
var httpClient = new HttpClient();
104+
var httpClient = GetHttpClient(ref token);
95105
if (!string.IsNullOrEmpty(token))
96106
{
97107
httpClient.DefaultRequestHeaders.Authorization =
@@ -136,7 +146,7 @@ public static async Task<T> SendAsync<T>(HttpRequestMessage request, string? tok
136146

137147
try
138148
{
139-
var httpClient = new HttpClient();
149+
var httpClient = GetHttpClient(ref token);
140150
if (!string.IsNullOrEmpty(token))
141151
{
142152
httpClient.DefaultRequestHeaders.Authorization =
@@ -192,7 +202,7 @@ public static async Task<HttpResponseMessage> SendJsonAsync<TJson>(HttpMethod ht
192202
request.Content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
193203
}
194204

195-
var httpClient = new HttpClient();
205+
var httpClient = GetHttpClient(ref token);
196206
if (!string.IsNullOrEmpty(token))
197207
{
198208
httpClient.DefaultRequestHeaders.Authorization =
@@ -235,7 +245,7 @@ public static async Task<TResult> SendJsonAsync<TResult, TJson>(HttpMethod httpM
235245
var jsonContent = JsonSerializer.Serialize(content);
236246
request.Content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
237247

238-
var httpClient = new HttpClient();
248+
var httpClient = GetHttpClient(ref token);
239249
if (!string.IsNullOrEmpty(token))
240250
{
241251
httpClient.DefaultRequestHeaders.Authorization =
@@ -321,4 +331,18 @@ private static void LogError(HttpMethod httpMethod, string url, Exception except
321331

322332
NewLogger.Logger.LogError(logMessage);
323333
}
334+
335+
private static HttpClient GetHttpClient(ref string? token)
336+
{
337+
if (string.IsNullOrEmpty(token))
338+
{
339+
return new HttpClient();
340+
}
341+
else
342+
{
343+
var clientInfo = HttpClientPool.GetClient(SharedName);
344+
clientInfo.InUse = false;
345+
return clientInfo.Client;
346+
}
347+
}
324348
}

0 commit comments

Comments
 (0)