问题描述
我可以直接使用 HttpClient.Timeout
设置 HttpClient
对象的超时,但是我最近读过有关 WebRequestHandler
类,它是 HttpClientHandler
的派生类。
I can set the timeout of my HttpClient
object directly with HttpClient.Timeout
but I've recently read about the WebRequestHandler
class which is a derivative of HttpClientHandler
.
WebRequestHandler
具有 ReadWriteTimeout
属性。与 HttpClient.Timeout
一起使用时,这将如何影响请求的操作?
WebRequestHandler
has a ReadWriteTimeout
property. How will this affect the operation of the request when used alongside HttpClient.Timeout
?
推荐答案
当您执行 SendAsync
时, HttpClient.Timeout
放在 CancellationTokenSource上
。这意味着此超时是针对整个异步操作的。
When you perform a SendAsync
the HttpClient.Timeout
is placed on the CancellationTokenSource
. This means this timeout is for the entire async operation.
另一方面,将 WebRequestHandler.ReadWriteTimeout
复制到 HttpWebRequest
在请求流上设置的 ReadTimeout
和 WriteTimeout
。因此,这更多的是流级别的超时,最终是套接字级别的超时。
On the other hand, WebRequestHandler.ReadWriteTimeout
is copied to the HttpWebRequest
where it is set on the request stream both ReadTimeout
and WriteTimeout
. So this is more a timeout at the stream level, which is ultimately a socket level timeout.
如果同时设置了两者,则该操作花费的时间超过 HttpClient.Timeout
总共将超时,并且如果从流中进行的读取或写入所花费的时间超过 WebRequestHandler.ReadWriteTimeout
,则还将超时。尽管我不确定所引发的超时异常是否有所不同。
If you set both, then if the operation takes more than HttpClient.Timeout
in total it will timeout, and if a read or write from the stream takes longer than WebRequestHandler.ReadWriteTimeout
it will also timeout. Though I am not sure if there is a difference in the timeout exceptions raised.
这篇关于HttpClient.Timeout和使用WebRequestHandler超时属性之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!