I'm using .NET HttpClient
to send requests to my server. I have set HttpClient.Timeout
property to 10 seconds so i'm getting a A task was cancelled
exception whenever the server fails to process my request in less than 10 seconds. Good until here.
However, if the server is shut down it takes for the HttpClient
around ~20 seconds to return a proper exception such as the one in the picture.
I would like to see this exception faster than 10 seconds to be able to distinguish between Server Down and Operation took too long scenarios. I can't find anything about this in msdn documentation. Is there any timeout that can be set on the HttpClient
?
Here's how i'm constructing the HttpClient
var webRequestHandler = new WebRequestHandler
{
UseProxy = false,
Proxy = null,
AllowPipelining = false,
ContinueTimeout = TimeSpan.Zero,
UseCookies = false,
AllowAutoRedirect = false,
};
var httpClient = new HttpClient(webRequestHandler);
httpClient.Timeout = timeout;
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "application/json");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", userAgent);
httpClient.DefaultRequestHeaders.ExpectContinue = false;