HttpClient如何正确处理超时? [英] How exactly are timeouts handled by HttpClient?

查看:698
本文介绍了HttpClient如何正确处理超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,可以在 WebRequestHandler.ReadWriteTimeout .

第一个只是整个请求/响应的超时,因此,如果下载/上传花费的时间更长,那么我很不走运,并且在传输过程中被切断,没有任何问题.可以通过将超时设置为Infinite来明显地克服这一点,但是我不确定这会带来什么后果.

The first one is just a timeout for the whole request/response, so if a download/upload takes longer than that, I am out of luck and get cut off in the middle of transfer, no questions asked. This can obviously be overridden by setting the timeout to Infinite, but I am not certain what consequences does that have.

现在,至少在我看来,后者(ReadWriteTimeOut)应该一直传播到

Now the latter (ReadWriteTimeOut) - at least to my understanding - should be propagated all the way down to NetworkStream, where it affects how long a request/response stream can block (be idle) until timeout.

HttpClient正在使用 HttpWebRequest 方法,但如此处:

The HttpClient is using asynchronous versions of the HttpWebRequest methods, but as stated here:

对于异步请求,客户端应用程序有责任实施自己的超时机制.

In the case of asynchronous requests, it is the responsibility of the client application to implement its own time-out mechanism.

根本没有说明他们要考虑哪种超时机制(网络流空闲超时?整个HTTP GET的超时?等等),这让我感到非常困惑.

It is not explained at all which time-out mechanism they have in mind (network stream idle timeout? timeout for the whole HTTP GET? etc...), so that makes me greatly confused.

所以我的问题是,HttpClient如何完全处理网络/协议超时?我想使用HttpClient上传/下载可能较大的文件,因此我将HttpClient.TimeOut设置为infinite.但是我担心这样做会冒无限期等待服务器/网络拒绝完成的事件的风险.

So my question is, how exactly does HttpClient handle network/protocol timeouts? I want to use HttpClient to upload/download potentially large files, so for that I set the HttpClient.TimeOut to infinite. But I am worried that by doing so, the app is at risk of waiting infinitely for some event that the server/network refuses to complete.

推荐答案

使用HttpClient下载文件时有2个选项

There are 2 options when you download a file using HttpClient

  1. 在Get请求上使用HttpCompletionOption.ResponseContentRead(这是默认的隐式选项).然后HttpClient.Timeout有效地应用于整个下载过程.我想您想到无限超时的情况就是如此

  1. Use HttpCompletionOption.ResponseContentRead on your Get request (that's a default implicit option). Then HttpClient.Timeout is effectively applied to the entire download procedure. I suppose that's the case when you thought about Infinite timeout

在您的Get请求上明确使用HttpCompletionOption.ResponseHeadersRead.然后HttpClient.Timeout仅应用于没有内容的Head响应. 之后,您可以自行下载内容管理超时时间,例如通过response.Content.ReadAsStreamAsync()

Explicitly use HttpCompletionOption.ResponseHeadersRead on your Get request. Then HttpClient.Timeout is applied only to getting Head response without content. After that you could download the content managing timeout at your own, e.g. by response.Content.ReadAsStreamAsync()

我想选项2是您想要的.只需设置一个HttpClient.Timeout即可在合理的时间内从服务器获得响应,然后下载内容

I suppose the option 2 is what you were seeking of. Just setup a HttpClient.Timeout to get response from server in reasonable time and then download content

这篇关于HttpClient如何正确处理超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆