tags:

views:

332

answers:

3

I am very surprised to see HttpWebRequest has no close method, but its counter-part HttpWebResponse has. It makes me a little bit confused and inconvenient. :-)

So, we only need to call Close on response and no need to treat with request? My concern is about leaks and better resource usage efficiency. I am using VSTS2008 + C# + .Net 3.5.

+10  A: 

Yes, you just need to call it on the response object.

A request does absolutely nothing on its own. It doesn't open up a socket or something. It just holds some data and you can just ignore it and throw it away if you don't need it (it's a pure managed resource and garbage collector will take care of it). Actual stuff happens after one of the GetResponse methods is called.

Mehrdad Afshari
Hi, is there any MS documentation or another "official" place explaining the inner workings of HttpWebRequest/Response?
tamberg
+1  A: 

The close method you refer to on HttpWebResponse serves to send the finished response to the Web Server. On the HttpWebRequest object, all content is available when you start processing, so there is no need to close and communicate with a server.

gimel
+2  A: 

An analogy:

If I want to talk to you and I am already sure I want to speak to you I will simply call your name (Request). However, is up to you to decide when, how and what to answer to me (Response). So you will have more control over the communication than me, I just started it.

Freddy