1

I need to inspect some HTTP headers and so I'm using C#'s HttpWebRequest and HttpWebResponse classes.

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 http://localhost:[Port]/ it doesn't connect.

I can see that it repeatedly is making a request to the server and eventually I get the exception WebException: The Operation has timed out. If I add request.KeepAlive = false' then I get the exception WebException: Unable to connect to the remote server.

So I'm wondering:

  1. Is something wrong with my code? (see below)
  2. How can I test HttpWebRequest and HttpWebResponse on localhost?

I've tried using the IP address in place of "localhost" but that didn't work (ex http://127.0.0.1:[port]/)

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);
        }
    }
} 
neuquen
  • 3,991
  • 15
  • 58
  • 78
  • You won't see any information in Chrome Dev tools since the request is made by the application, not by the browser. – Marthijn Dec 31 '13 at 19:22
  • @Marthijn That makes sense. Is there a tool or plugin of some kind that can intercept header info made from the application? I was hoping that I could just output the response by using `Debug.Write` and take a look at what is sent in the output window of VS2012. – neuquen Dec 31 '13 at 19:26
  • Just set breakpoints in your application, see this question for a screenshot: http://stackoverflow.com/questions/2371835/how-to-read-http-header-from-response-using-net-httpwebrequest-api – Marthijn Dec 31 '13 at 19:44

1 Answers1

1

Have you tried Fiddler? Fiddler will show you all the HTTP Requests, all headers, response status, with different data views like raw, hex, image etc, a timeline view, HTTPS Connects, pretty much everything.

There are also Firefox extensions like httpfox. But I strongly recommend you try out Fiddler.

Shiva
  • 20,575
  • 14
  • 82
  • 112
  • I haven't but I've heard the name thrown around a lot. I'll have to take a look at it. – neuquen Dec 31 '13 at 19:38
  • Fiddler just seems kind of overkill though. I'd really just like to know if the code will successfully connect to the server and if when connected, will I be able to read the header info and do the logic accordingly... – neuquen Dec 31 '13 at 19:44
  • Will Fiddler be able to tell me why my request keeps timing out? – neuquen Dec 31 '13 at 20:17