Search code examples
c#.nethttpwebrequesthttpwebresponse

HttpWebResponse - disposing of a connection properly


I'm working on a download manager in C# and I'm making using of multiple http requests and was wondering how can one make sure a connection properly closed?

Is it enough to call Dispose on the response stream? Do I need to call Close as well? Not sure where things could possibly could go wrong but at some point a web site would become unresponsive.

Thanks!


Solution

  • Wrap your HttpWebResponse in a using block:

    using(HttpWebResponse response = request.GetResponse())
    {
        // do stuff here
    } // response object is automatically disposed of here.