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 wron开发者_Go百科g but at some point a web site would become unresponsive.
Thanks!
Wrap your HttpWebResponse in a using block:
using(HttpWebResponse response = request.GetResponse())
{
    // do stuff here
} // response object is automatically disposed of here. 
As Kyle mentioned, wrap your HttpWebResponse in a using block. But if GetResponse() throws an exception (which happens on a 404 response, for instance), you need to grab the HttpWebResponse in the exception.
HttpWebResponse webResponse = null;
try {
    webResponse = (HttpWebResponse)webRequest.GetResponse();
} catch (WebException e) {
    webResponse = (HttpWebResponse)e.Response;
    if (webResponse == null) {
        // Handle this.
    }
}    
using (webResponse) {
    // Process the response.
}
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论