title: How to close underlying connections after catch httpwebrequest timeout
body: <p>My asp.net application sending httpwebrequest to remote REST server and waiting for the response, and I found there are lots of same error message like this:</p>
  
  <blockquote>
    <p>System.Net.WebException: The operation has timed-out.       at
    System.Net.HttpWebRequest.GetResponse()</p>
  </blockquote>
  
  <p>Is that possible that after I catch this exception and close the underlying http connection directly? or I don't really have to do so since I already set keepalive to false?</p>
  
  <p>Thanks.</p>
  
  <p>Actually another questions is if the timeout exception always happened at <code>System.Net.HttpWebRequest.GetResponse(),</code> does that mean application is waiting for the response from remote server and could not get response until time out. what could be the possible reason, network connection not stable? remote server not response? any other possible reasons?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>System.Net.HttpWebResponse httpWebResponse = null;
  System.IO.Stream stream  = null;
  XmlTextReader xmlTextReader  = null;
  try
  {
      System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(request);
      httpWebRequest.ReadWriteTimeout = 10000;
      httpWebRequest.Timeout = 10000;
      httpWebRequest.KeepAlive = false;
      httpWebRequest.Method = "GET";
      httpWebResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
      stream = httpWebResponse.GetResponseStream();
      xmlTextReader = new  XmlTextReader(stream);
      xmlTextReader.Read();
      xmlDocument.Load(xmlTextReader);
      //Document processing code.
      //...
  }
  catch
  {
      //Catch blcok with error handle
  }
  finally
  {
      if (xmlTextReader != null)
          xmlTextReader.Close();
      if (httpWebResponse != null)
          httpWebResponse.Close();
      if (stream != null)
          stream.Close();
  }
  </code></pre>