Search code examples
c#.nettimeoutdotnet-httpclient

Dynamically changing HttpClient.Timeout in .NET


I need to change a HttpClient.Timeout property after it made a request(s). When I try, I get an exception:

This instance has already started one or more requests. Properties can only be modified before sending the first request.

Is there any way to avoid this?


Solution

  • There isn't much you can do to change this. This is just default behavior in the HttpClient implementation.

    The Timeout property must be set before the GetRequestStream or GetResponse method is called. From HttpClient.Timeout Remark Section

    In order to change the timeout, it would be best to create a new instance of an HttpClient.

    client = new HttpClient();
    client.Timeout = 20; //set new timeout