HttpClient如何正确处理超时?

编程入门 行业动态 更新时间:2024-10-19 12:39:00
本文介绍了HttpClient如何正确处理超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

因此,可以在 HttpClient : HttpClient.TimeOut 和 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)应该一直传播到 NetworkStream ,它会影响请求/响应流可以阻塞(空闲)直到超时的时间.

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

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

  • 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如何正确处理超时?

    本文发布于:2023-11-24 04:36:40,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1623994.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:正确处理   HttpClient

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!