简体   繁体   English

HttpWebResponse:关闭流

[英]HttpWebResponse: closing the stream

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. 我从HttpWebRequest (使用Jeff Richter的CCR包装器的修改版)获得响应,然后检查一些标头以决定是否继续下载。 Sometimes I might not want to continue, so I consequently issue response.Close and request.Abort . 有时我可能不想继续,因此我发出了response.Closerequest.Abort Is it necessary to issue GetResponseStream then to close the stream, or is this implicit when one calls response.Close ? 是否有必要发出GetResponseStream然后关闭流,还是在有人调用response.Close时隐式存在?

After issuing GetResponse, the docs state : 发出GetResponse后, 文档状态为

You must call the Close method to close the stream and release the connection. 您必须调用Close方法以关闭流并释放连接。 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. 这似乎是资源泄漏的最有力的候选人,但想知道是否还有其他人对此问题有经验。

As an aside: is it safe to GetResponseStream twice in the assumption that it is the same stream? 顺便说一句:假设相同的流,两次GetResponseStream是否安全?

Calling HttpWebResponse.Close implicity closes the response stream. 调用HttpWebResponse.Close隐式关闭响应流。

From the documentation: 从文档中:

The Close method closes the response stream and releases the connection to the resource for reuse by other requests Close方法关闭响应流并释放与资源的连接,以供其他请求重用

You must call either the Stream.Close or the HttpWebResponse.Close method to close the stream and release the connection for reuse. 您必须调用Stream.Close或HttpWebResponse.Close方法来关闭流并释放连接以供重用。 It is not necessary to call both Stream.Close and HttpWebResponse.Close, but doing so does not cause an error. 不必同时调用Stream.Close和HttpWebResponse.Close,但是这样做不会导致错误。 Failure to close the stream can cause your application to run out of connections. 无法关闭流会导致您的应用程序用尽连接。

And for your double-GetResponseStream question, although the documentation doesn't explicitly mention it, it will always return the same stream object no matter how many times you call it. 对于您的Double-GetResponseStream问题,尽管文档中没有明确提及它,但无论您调用它多少次,它都将始终返回相同的流对象。

Actually, a call to webResponse.Close() will close the response Stream. 实际上,对webResponse.Close()的调用将关闭响应流。

The response is IDisposable, I advice you an using statement. 响应是IDisposable,我建议您使用using语句。

do something like WCF Connections 做类似WCF连接的操作

//Done with the service, let's close it.
try
{
   if (client.State != System.ServiceModel.CommunicationState.Faulted)
   {
      client.Close();
   }
}
catch (Exception ex)
{
   client.Abort();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM