You are not following the official HttpClient documentation. Register the HttpClient as a service and use dependency injection. You should have one HttpClient for each services and not dispose the HttpClient on each use. This can cause the connection issues described above.
Make HTTP requests using IHttpClientFactory in ASP.NET Core
Hi @AgaveJoe
I'm in learning process, if you don't mind, can you give me a simple example for doing HTTP GET?
I have tried below:
program.cs
builder.Services.AddHttpClient();
class1.cs
private readonly IHttpClientFactory _httpClientFactory;
public async Task<bool> test1
for (int pageNo = 1; pageNo <= 10; pageNo++)
var httpRequestMessage = new HttpRequestMessage(
HttpMethod.Get,
$"https://xxxx.xxx.com/test/?per_page=500&page={pageNo}")
Headers = { { HeaderNames.Authorization, "Token " + token } }
var httpClient = _httpClientFactory.CreateClient();
var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage);
if (httpResponseMessage.IsSuccessStatusCode)
using var contentStream =
await httpResponseMessage.Content.ReadAsStreamAsync();
I face the same error and I already followed the instruction in the official website but still get that error sometimes
May I know if I can find other solution ?
Definitely use IHttpClientFactory to create HttpClient - the wrong way of initialization.
It caused so much trouble for me.
Nice example here for Net MAUI
https://github.com/yogigrantz/MAUIWithHttpClientFactory/tree/main/MAUIMultiPage
builder.Services.AddHttpClient();