7

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 ?

Pacerier
  • 86,231
  • 106
  • 366
  • 634
  • No you don't need to close it, using is a wrapper for try{ Stream stream; ... }catch {..} finally { if (stream != null) { stream.Close(); } } – Alan Turing Sep 12 '11 at 10:54
  • 1
    @Artur: I think you've misunderstood the question - it's not about whether you need to explicitly call Close, it's whether closing the StreamReader (via a using statement) is enough to close the stream and the WebResponse. – Jon Skeet Sep 12 '11 at 11:08
  • @Artur: `using` finally calls `Dispose()` on the object, not `Close()`. Disposing an object should close open resources, but `using` doesn't do so directly. – Suncat2000 May 09 '19 at 11:44

2 Answers2

9

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • @Artur: Which bit? I'm very well aware of what a Using statement means in IL - which bit of my answer goes against that? – Jon Skeet Sep 12 '11 at 11:06
2

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

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
  • Doing this will show up the bad knowledge of C# language specification, ECMA standart. Again, using statement is a syntax shugar wrapper for try { Stream stream; ... } catch {..} finally { if (stream != null) { stream.Close(); } } block. – Alan Turing Sep 12 '11 at 10:59
  • I agree you but I was just reporting what MSDN says about that – Massimiliano Peluso Sep 12 '11 at 11:53