Re-using HttpClient but with a different Timeout setting per request?

Under the hood, HttpClient just uses a cancellation token to implement the timeout behavior. You can do the same directly if you want to vary it per request:

using var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(30));
await httpClient.GetAsync("http://www.google.com", cts.Token);

Note that the default timeout for HttpClient is 100 seconds, and the request will still be canceled at that point even if you’ve set a higher value at the request level. To fix this, set a “max” timeout on the HttpClient, which can be infinite:

httpClient.Timeout = System.Threading.Timeout.InfiniteTimeSpan;

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)