Change default timeout
client.Timeout = 5*1000;
doesnt work because client.Timeout
expects something of type: System.TimeSpan
I changed the Timeout value using:
client.Timeout = TimeSpan.FromSeconds(10); // Timeout value is 10 seconds
You can use other methods as well:
- FromDays
- FromHours
- FromMilliseconds
- FromMinutes
- FromSeconds
- FromTicks
Just for FYI:
Default value of Timeout
property is 100 seconds
The default timeout of an HttpClient
is 100 seconds.
HttpClient Timeout
You can adjust to your HttpClient
and set a custom timeout duration inside of your HttpService
.
httpClient.Timeout = 5000;
HttpClient Request Timeout
You could alternatively define a timeout via a cancellation token CancellationTokenSource
using (var cts = new CancellationTokenSource(new TimeSpan(0, 0, 5))
{
await httpClient.GetAsync(url, cts.Token).ConfigureAwait(false);
}
A few notes:
- Making changes inside of the
HttpClient
will affect all requests. If you want to make it per request you will need to pass through your desired timeout duration as a parameter. - Passing an instance of
CancellationTokenSource
will work if it's timeout is lower thanTimeout
set by theHttpClient
andHttpClient
's timeout is not infinite. Otherwise, theHttpClient
's timeout will take place.