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
{
///
/// Displays HTTP method, and count of Headers collection
///
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:
And here's how I built the DLL, which I copied into the webapp's bin directory:
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.
Thomas1
Member
625 Points
125 Posts
Re: How to see the HTTP headers?
Dec 30, 2003 10:26 PM|LINK
using System; using System.Web; using System.Collections.Specialized; namespace mypractice01 { /// /// Displays HTTP method, and count of Headers collection /// 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: And here's how I built the DLL, which I copied into the webapp's bin directory: Finally, here's a sample of what I'm seeing at the end of each page: 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.