title: HttpWebRequest/Response on localhost (WebException: operation timed out)
body: <p>I need to inspect some HTTP headers and so I'm using C#'s <code>HttpWebRequest</code> and <code>HttpWebResponse</code> classes.</p>
  
  <p>I'm developing using Visual Studio 2012 and would like to test out the request and response on my localhost so I can see what kind of headers I'm dealing with (I use devtools in Chrome so I can see there, but I want to make sure my code is returning the proper values).  When I simply put in <code>http://localhost:[Port]/</code> it doesn't connect.</p>
  
  <p>I can see that it repeatedly is making a request to the server and eventually I get the exception <code>WebException: The Operation has timed out</code>.  If I add <code>request.KeepAlive = false'</code> then I get the exception <code>WebException: Unable to connect to the remote server</code>.</p>
  
  <p>So I'm wondering:</p>
  
  <ol>
  <li>Is something wrong with my code? (see below)</li>
  <li>How can I test <code>HttpWebRequest</code> and <code>HttpWebResponse</code> on localhost?</li>
  </ol>
  
  <p>I've tried using the IP address in place of "localhost" but that didn't work (ex <code>http://127.0.0.1:[port]/</code>)</p>
  
  <p>Code:</p>
  
  <pre><code>   public class AuthorizationFilterAttribute : ActionFilterAttribute
     {
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          base.OnActionExecuting(filterContext);
  
          string url = "http://127.0.0.1:7792/";
          string responseString;
  
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
          //request.KeepAlive = false;
          //request.AllowAutoRedirect = false;
  
          System.Diagnostics.Debug.Write(request);
  
  
          try
          {
              using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
              {
                  using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                  {
                      responseString = reader.ReadToEnd();
                      System.Diagnostics.Debug.Write(responseString);
                  }
              }
          }
          catch (Exception e)
          {
              System.Diagnostics.Debug.Write(e.Message);
              System.Diagnostics.Debug.Write(e.Source);
          }
      }
  } 
  </code></pre>