- 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();
}