Dynamically changing HttpClient.Timeout in .NET

AsValeO picture AsValeO · May 21, 2014 · Viewed 15.6k times · Source

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?

Answer

Andrew Smith picture Andrew Smith · May 21, 2014

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