locked
How to see the HTTP headers? RRS feed

  • Question

  • User-583980620 posted
    I'm curious as regards the HTTP headers that are being sent back-and-forth between my ASP.NET server and the IE client. Would I do this via an HTTP module? I'm guessing that I would intercept the HttpApplication.EndRequest event, and then use a few context.response.write's to show things like context.request.httpmethod (GET/POST/etc), and maybe the collection of context.request.headers. Is any of this close? Will this show me both incoming & outgoing headers? I think what I just described will only show me outgoing headers (getting sent to the client), I'm not sure it'll show me the header that comes into the server. Thanks for any ideas/help!!
    Tuesday, December 30, 2003 4:56 PM

All replies

  • User-583980620 posted
    Okay, I wrote it... but I'm guessing that I'm only seeing the headers being sent from the client to the server. I guess first I should ask if there are other headers to be seen? What about the headers that are being sent from the server to the client- like a response.redirect/302? I'm interested in seeing those, too. Here's the code for the ShowHTTPHandlers.cs ... using System; using System.Web; using System.Collections.Specialized; namespace mypractice01 { /// <summary> /// Displays HTTP method, and count of Headers collection /// </summary> public class ShowHTTPHeaders : IHttpModule { public void Init (HttpApplication Application) { Application.EndRequest += new EventHandler(Application_EndRequest); } private void Application_EndRequest(object sender, EventArgs e) { HttpApplication Application = (HttpApplication) sender; Application.Context.Response.Write("HTTP Method is: " + Application.Context.Request.HttpMethod + "
    "); Application.Context.Response.Write("Header count is: " + Application.Context.Request.Headers.Count + "
    "); Application.Context.Response.Write("
    "); /* Begin quick & dirty paste from MSDN */ int loop1, loop2; NameValueCollection coll; // Load Header collection into NameValueCollection object. coll=Application.Context.Request.Headers; // Put the names of all keys into a string array. String[] arr1 = coll.AllKeys; for (loop1 = 0; loop1<arr1.Length; loop1++) { Application.Context.Response.Write("Key: " + arr1[loop1] + "
    "); // Get all values under this key. String[] arr2=coll.GetValues(arr1[loop1]); for (loop2 = 0; loop2<arr2.Length; loop2++) { Application.Context.Response.Write("Value " + loop2 + ": " + arr2[loop2] + "
    "); } } /* End of quick & dirty paste from MSDN */ } public void Dispose() {} } }
    And here's the section I added to my app's config.web file: <httpModules> <!-- format: name="AFriendlyNameForThis" type="namespace.classname, assemblyname" --> <add name="ShowHTTP" type="mypractice01.ShowHTTPHeaders,ShowHTTPHeaders"></add> </httpModules> And here's how I built the DLL, which I copied into the webapp's bin directory: C:\WINNT\Microsoft.NET\Framework\v1.1.4322>csc /t:library /r:System.dll,System.Web.dll \\testserver1\wwwroot$\mypractice01\ShowHTTPHeaders.cs Finally, here's a sample of what I'm seeing at the end of each page: HTTP Method is: GET Header count is: 9 Key: Cache-Control Value 0: no-cache Key: Connection Value 0: Keep-Alive Key: Accept Value 0: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */* Key: Accept-Encoding Value 0: gzip, deflate Key: Accept-Language Value 0: en-us Key: Cookie Value 0: ASP.NET_SessionId=prc32w5ahtnjxsg5rkaas213 Key: Host Value 0: testserver1 Key: Referer Value 0: http://testserver1/mypractice01/WebForm3.aspx Key: User-Agent Value 0: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.1.4322) Now if I could see what the server sends to the browser, I think I'll be happy. Is streaming to a text file a good solution? (This is just for practice/learning, none of this will be used in the final version.) Or should I maybe store the header info in the Session state, and retrieve it with a separate aspx page? Is it even possible to see what the server sends to the browser? Like I said, I want to see the 302/Redirect command appear in my output.
    Tuesday, December 30, 2003 6:26 PM
  • User1220193301 posted
    Take a look at: Application.Context.Response.StatusCode Application.Context.Response.StatusDescription You might need to check the values of these properties inside the "PreSendRequestHeaders" event handler method because that event is fired right before buffered response headers sent. I hope this helps
    Wednesday, December 31, 2003 12:19 PM
  • User-583980620 posted
    Thanks for the reply! I tried it out, and I'm still seeing the same header info from the PreSendRequestHeaders event as I am from the EndRequest event. I'm guessing that since the browser's redirecting itself, I never get to "see" the headers for the redirect. I guess I'll have to set up a stream (to a text file)? Or is there a better way?
    Sunday, January 4, 2004 8:19 PM
  • User-1962478842 posted
    You could use a packet sniffer. Ethereal is one that I use.
    Tuesday, January 13, 2004 8:12 AM
  • User-1645088262 posted
    There are a couple of IE add-ins that you can use to show the headers: http://www.httpwatch.com/ http://www.blunck.info/iehttpheaders.html These work on any site and show you the headers being sent and received (from the client). Don't forget that these may differ slightly than the headers that the server sends and receives (if the request goes through a proxy server).
    Tuesday, January 13, 2004 10:22 AM