我们需要关闭 System.Net.WebRequest 的 ResponseStream 吗?

发布于 2024-12-03 21:01:58 字数 726 浏览 0 评论 0原文

我想知道我最终会从这段代码中获得任何未关闭的流:

   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.WebRequestResponseStream 吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东风软 2024-12-10 21:01:58

需要关闭响应流或者您需要关闭响应。请注意,关闭包装 Stream 的 StreamReader 无论如何都会关闭流,因此第一个版本应该没问题。 (请注意,我认为“使用Using语句进行处置”在语义上等于“在finally块中关闭” - 显式调用Close而不是仅仅处置流或响应没有任何好处.)

相信关闭流就足够了 - 您也不需要关闭响应 - 事实上这就是 MSDN 的规定,但为了清楚起见,我个人会这样做:(

Public Function [Get](ByVal url As String) As String
    Using response = WebRequest.Create(url).GetResponse
        Using reader = New System.IO.StreamReader(response.GetResponseStream)
            Return reader.ReadToEnd
        End Using
    End Using
End Function

有一个理论 这样做的好处是,如果 GetResponse 返回成功,但 GetResponseStreamStreamReader 构造函数抛出异常(我不知道),它将关闭响应。没想到会这样有任何实际影响。)

如果您不关闭任何东西,您可能很容易在将来对同一主机的请求中遇到超时 - “打开”响应本质上将占用与该主机的连接,默认情况下,每个主机最多只能打开两个连接。这是超时的一个非常常见的原因——有很多这样的问题,人们因为没有关闭任何东西而超时。

You either need to close the response stream or you need to close the response. Note that closing a StreamReader wrapping a Stream 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 calling Close 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:

Public Function [Get](ByVal url As String) As String
    Using response = WebRequest.Create(url).GetResponse
        Using reader = New System.IO.StreamReader(response.GetResponseStream)
            Return reader.ReadToEnd
        End Using
    End Using
End Function

(There's a theoretical benefit here that it will close the response if GetResponse returns successfully but either GetResponseStream or the StreamReader 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.

雪花飘飘的天空 2024-12-10 21:01:58

没有必要在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文