views:

62

answers:

1

Hi all,

I was trying to get clarification on this :

Method-1:

Dim request = CreateRequest(uri) //some uri
Dim response = 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

Whats the difference between the Methods mentioned above and what might be the possible problem ?

Help in this regard would be greatly appreciated .. :)

Thanks,
Mahesh Velaga.

A: 

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.

Joey
Hi Johannes .. I tried reflector even before posting here .. but if you look at the close() in reflector it doesn't have any code in that function, you are right that the Dispose() and close() are different, but why am I getting this working on one machine and not on the other as per my question
Mahesh Velaga
Thanks for the reply though .. :), so all using blocks call just the Dispose() or anything else ?
Mahesh Velaga
`Using` should just call `Dispose()`, nothing else. After all, it requires an `IDisposable`, so it can't know about `Close()`. You can look at Reflector, though, to find that.
Joey
+1 for the comment clarification ... and accepted the answer for providing the info in right direction .. thanks :)
Mahesh Velaga