- Use a single instance of HttpClient.
private static readonly HttpClient httpClient = new HttpClient();
- Define the max concurrent requests per URL.
ServicePointManager.FindServicePoint(new Uri("http://abc.com")).ConnectionLimit = 512;
- Avoid port exhaustion – Don’t use HttpClient as a request queue.
- Only use DefaultRequestHeaders for headers that don’t change.
using (var request = new HttpRequestMessage(HttpMethod.Get, GetRandomNumberUrl))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", Token);
var response = await HttpClient.SendAsync(request);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者