I have some code like, this code is used very heavily:
using (HttpWebResponse r = _GetHttpWebResponse(uri, body, method, contentType, headers)) {
/* do something with the response */
/* call r.Close() explicitly? */
}
The code works fine today, but the connections to the server stay open for quite some time. (checked using TCPView)
Is there a benefit to calling the Close()
method explicitly? Is it recommended, or maybe recommended not to do it, and why?
When Dispose()
is called on WebResponse
(HttpWebReponse
's base class), it calls it's Close()
method for you. A quick glance using Reflector confirms this.
Edit (in response to comment): If it's called for you already, why call it explicitly? For the sake of clarity? I think if people understand the using (X x = ...)
statement, they'll understand that it is closing the underlying connection. You gain nothing by calling it explicitly in this case.