kofifus
kofifus

Reputation: 19275

correctly disposing HttpWebResponse in exception

consider the following function taken from Microsoft doc:

void webREquest(url) {
  // taken from https://msdn.microsoft.com/en-us/library/system.net.webexception.response(v=vs.110).aspx
  try {
     // Create a web request for an invalid site. Substitute the "invalid site" strong in the Create call with a invalid name.
     HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url);

    // Get the associated response for the above request.
    HttpWebResponse myHttpWebResponse = (HttpWebResponse) myHttpWebRequest.GetResponse();
    myHttpWebResponse.Close();
  }
  catch(WebException e) {
    Console.WriteLine("This program is expected to throw WebException on successful run."+
              "\n\nException Message :" + e.Message);
    if(e.Status == WebExceptionStatus.ProtocolError) {
      Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
      Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
    }
  }
  catch(Exception e) {
    Console.WriteLine(e.Message);
  }
}

I have a few questions about the way objects are disposed or indisposed:

I need to make sure absolutely no connection stays after the function exits ...

Upvotes: 1

Views: 575

Answers (1)

Dhejo
Dhejo

Reputation: 71

myHttpWebRequest is not directly closed/disposed - is this really unnecessary? When is the request closed in this case? how can I make sure it is closed as soon as possible?

Yes, but considering the class does not implement a IDisposable interface it might seem that it is ok not to.

in the WebException catch block, is it not necessary to somehow dispose/close e.Response (which is of type HttpWebResponse)? if not where/how it is disposed?

I would recommend closing it in the finally block to handle both exception and non-expection scenario

Wouldn't it be better to have a using clause around myHttpWebRequest and myHttpWebResponse? but if so, would that make e.Response in the exception handler inaccessible?

Unfortunately HttpWebRequest does not implement IDisposable so you cannot use using in this case

What is the meaning of the "This program is expected to throw WebException on successful run" above?

It is mentioned in the comment that request is made to an invalid site so it is expected to throw exception

void webREquest(string url)
        {
            // taken from https://msdn.microsoft.com/en-us/library/system.net.webexception.response(v=vs.110).aspx
            HttpWebRequest myHttpWebRequest = null;
            HttpWebResponse myHttpWebResponse = null;
            try
            {
                // Create a web request for an invalid site. Substitute the "invalid site" strong in the Create call with a invalid name.
                myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);

                // Get the associated response for the above request.
                myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                myHttpWebResponse.Close();
            }
            catch (WebException e)
            {
                Console.WriteLine("This program is expected to throw WebException on successful run." +
                          "\n\nException Message :" + e.Message);
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
                    Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                //close on myHttpWebResponse
                myHttpWebResponse?.Close();

                //mark myHttpWebRequest for collection
                myHttpWebRequest = null;
            }
        }

Upvotes: 1

Related Questions