What is the difference between HttpClient.Timeout and using WebRequestHandler timeout properties?

I can set the timeout of my HttpClient object directly using HttpClient.Timeout , but I recently read about the WebRequestHandler , which is derived from HttpClientHandler .

WebRequestHandler has a ReadWriteTimeout property. How will this affect the operation of the request when used next to HttpClient.Timeout ?

+7
source share
3 answers

When you execute SendAsync , the HttpClient.Timeout is placed in the CancellationTokenSource . This means that this timeout is for the entire async operation.

On the other hand, WebRequestHandler.ReadWriteTimeout copied to HttpWebRequest , where it is set in the request stream of both ReadTimeout and WriteTimeout . So this is more of a thread level timeout, which is ultimately a socket level timeout.

If you set both parameters, then if the operation takes longer than HttpClient.Timeout , then it will be a timeout, and if reading or writing from the stream takes longer than WebRequestHandler.ReadWriteTimeout , this will also lead to a timeout. Although I'm not sure if there is a difference in latency exceptions.

+16
source

WebRequestHandler.ReadWriteTimeout - Gets or sets the timeout in milliseconds when writing a request or reading a response from the server.

HttpClient.Timeout - Gets or sets the TimeSpan to wait for the request to time out.

Here WebRequestHandler is a wrapper over HTTPClient WebRequestHandler comes from HttpClientHandler , but adds properties that are usually only available on the full version of .NET. In conclusion, this is more like the same thing.

For more information, refer to this link - http://blogs.msdn.com/b/henrikn/archive/2012/08/07/httpclient-httpclienthandler-and-httpwebrequesthandler.aspx

+1
source

System.Net.HttpClient can set the timeout property as above

 var client = new HttpClient(); client.Timeout.Add(new TimeSpan(1,0,0)); 
-one
source

All Articles