我们需要关闭 System.Net.WebRequest 的 ResponseStream 吗?
我想知道我最终会从这段代码中获得任何未关闭的流:
Public Function [Get](ByVal url As String) As String
Using reader = New System.IO.StreamReader(System.Net.WebRequest.Create(url).GetResponse.GetResponseStream)
Return reader.ReadToEnd
End Using
End Function
那怎么样:
Public Function Get2(ByVal url As String) As String
Using stream = System.Net.WebRequest.Create(url).GetResponse.GetResponseStream
Using reader = New System.IO.StreamReader(stream)
Return reader.ReadToEnd
End Using
End Using
End Function
基本上,我们需要关闭 System.Net.WebRequest
的 ResponseStream
吗?
I was wondering will I end up having any unclosed streams from this code:
Public Function [Get](ByVal url As String) As String
Using reader = New System.IO.StreamReader(System.Net.WebRequest.Create(url).GetResponse.GetResponseStream)
Return reader.ReadToEnd
End Using
End Function
What about this:
Public Function Get2(ByVal url As String) As String
Using stream = System.Net.WebRequest.Create(url).GetResponse.GetResponseStream
Using reader = New System.IO.StreamReader(stream)
Return reader.ReadToEnd
End Using
End Using
End Function
Basically, do we need to close the System.Net.WebRequest
's ResponseStream
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要关闭响应流或者您需要关闭响应。请注意,关闭包装
Stream
的 StreamReader 无论如何都会关闭流,因此第一个版本应该没问题。 (请注意,我认为“使用Using语句进行处置”在语义上等于“在finally块中关闭” - 显式调用Close
而不是仅仅处置流或响应没有任何好处.)我相信关闭流就足够了 - 您也不需要关闭响应 - 事实上这就是 MSDN 的规定,但为了清楚起见,我个人会这样做:(
有一个理论 这样做的好处是,如果
GetResponse
返回成功,但GetResponseStream
或StreamReader
构造函数抛出异常(我不知道),它将关闭响应。没想到会这样有任何实际影响。)如果您不关闭任何东西,您可能很容易在将来对同一主机的请求中遇到超时 - “打开”响应本质上将占用与该主机的连接,默认情况下,每个主机最多只能打开两个连接。这是超时的一个非常常见的原因——有很多这样的问题,人们因为没有关闭任何东西而超时。
You either need to close the response stream or you need to close the response. Note that closing a
StreamReader
wrapping aStream
will close the stream anyway, so the first version should be okay. (Note that I'm deeming "dispose with a Using statement" to be semantically equal to "close in a finally block" - there's no benefit in explicitly callingClose
instead of just disposing of the stream or response.)I believe that closing the stream is good enough - that you don't need to close the response as well - and indeed that's what MSDN states, but personally I'd do so for clarity:
(There's a theoretical benefit here that it will close the response if
GetResponse
returns successfully but eitherGetResponseStream
or theStreamReader
constructor throws an exception. I don't expect that to have any practical implications.)If you don't close anything, you could very easily run into timeouts in future requests to the same host - the "open" response will essentially hog the connection to that host, and by default there's a limit of two open connections per host. This is a very common cause of timeouts - there are lots of SO questions where folks are getting timeouts due to not closing anything.
没有必要在 WebResponse 上调用 Close 方法,但这样做并没有什么害处
http://msdn.microsoft.com/en-us/library/debx8sh9.aspx
It is not necessary to call the Close method on the WebResponse, but doing so is not harmful
http://msdn.microsoft.com/en-us/library/debx8sh9.aspx