HttpWebResponse:关闭流 [英] HttpWebResponse: closing the stream

查看:230
本文介绍了HttpWebResponse:关闭流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 HttpWebRequest (使用修改后的版本 Jeff Richter的CCR包装器),然后检查一些标题以决定是否继续下载.有时我可能不想继续,因此我发出了 response.Close request.Abort .是否有必要发布 GetResponseStream 然后关闭流,或者当有人调用响应.关闭?

I'm getting the response from an HttpWebRequest (using a modified version Jeff Richter's CCR wrappers), then inspecting a few of the headers in order to decide whether or not to continue the download. Sometimes I might not want to continue, so I consequently issue response.Close and request.Abort. Is it necessary to issue GetResponseStream then to close the stream, or is this implicit when one calls response.Close?

在发布GetResponse之后,文档状态:

After issuing GetResponse, the docs state:

必须调用Close方法以关闭流并释放连接.否则可能会导致您的应用程序用尽连接.

You must call the Close method to close the stream and release the connection. Failure to do so may cause your application to run out of connections.

那么这是否意味着一旦我们收到响应,就必须获取并关闭流?

So does this mean that once we have a response, then it is obligatory to get the stream and close it?

我们看到一些相当奇怪的问题,挂起的下载最终淹没了系统.这似乎是资源泄漏的最有力的候选人,但想知道是否还有其他人对此问题有经验.

We're seeing some fairly strange issues where hung downloads are eventually swamping the system. This seems like the strongest candidate for a resource leak, but wonder if anyone else has experience with this issue.

顺便说一句:假设GetResponseStream是相同的流,两次安全吗?

As an aside: is it safe to GetResponseStream twice in the assumption that it is the same stream?

推荐答案

实际上,对webResponse.Close()的调用将关闭响应流.

Actually, a call to webResponse.Close() will close the response Stream.

响应是IDisposable,我建议您使用using语句.

The response is IDisposable, I advice you an using statement.

这篇关于HttpWebResponse:关闭流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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