Does HttpResponse work in a "using" block with out a explicit response.close()
I was trying to get clarification on this:
Method-1:
Dim request = CreateRequest(uri) //some uri
Dim re开发者_StackOverflow社区sponse = DirectCast(request.GetResponse, HttpWebResponse)
response.Close()
Method-2:
Dim request = Createrequest(uri)
Using response = DirectCast(request.GetResponse, HttpWebResponse)
End Using
When I used both Method-1 and Method-2 on my local machine to connect and get a response from a remote machine X, both of them worked properly.
When I used this code on a remote machine Y to get response from X, only the Method-1 is working, and for Method-2, I am getting
System.Net.WebException: The operation has timed out
What's the difference between the Methods mentioned above and what might be the possible problem ?
Using
simply translates into a Try
/Finally
block which calls .Dispose()
in the Finally
block. You can use Reflector to find what code is generated. You can also use it to take a look at what a method does. In this case, the Dispose()
method on HttpWebResponse
does not do the same as Close()
which means it has in fact semantic differences here.
Using
usually has the benefit of releasing resources used by an object immediately when it goes out of scope. This is useful for things like GDI+ or file handles, but in the case of HttpWebResponse
it might be a little bit different. Not knowing enough about that particular object my guess would be that Close()
ing it doesn't really release any resources, so there is no need for Dispose()
calls Close()
too. And maybe there are valid reasons for that behavior.
精彩评论