by Njål

Complete Dump of Asp.net WebRequest

 


Alternate way of dumping logs

 

Here’s a simple but very handy method that lists out all relevant data for a Asp.net web request. Posted Parameters, Query String, Http Headers, Cookies and Session variables are returned – and can be logged out to file/console etc.

Note that this static method can be called anywhere from your code – it does not have to be inside a aspx/ashx etc. The only requirement is that the code must run inside IIS and a web request must be assosiated with the current thread.

 

 

 

public static string DumpRequest()
{
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("");
    sb.AppendLine("-------------  PostedParameters --------------");
    foreach (string key in HttpContext.Current.Request.Form.Keys.Cast<string>().OrderBy(f => f))
    {
        sb.AppendLine(key.PadLeft(40, ' ') + " : " + HttpContext.Current.Request.Params[key]);
    }

    sb.AppendLine("");
    sb.AppendLine("-------------  Query String ------------------");
    foreach (string key in HttpContext.Current.Request.QueryString.Keys.Cast<string>().OrderBy(f => f))
    {
        sb.AppendLine(key.PadLeft(40, ' ') + " : " + HttpContext.Current.Request.QueryString[key]);
    }

    sb.AppendLine("");
    sb.AppendLine("-------------  Http Headers -------------------");
    foreach (string key in HttpContext.Current.Request.Headers.Keys.Cast<string>().OrderBy(f => f))
    {
        sb.AppendLine(key.PadLeft(40, ' ') + " : " + HttpContext.Current.Request.Headers[key]);
    }

    sb.AppendLine("");
    sb.AppendLine("--------------  Cookies -------------------------");
    foreach (string key in HttpContext.Current.Request.Cookies.Keys.Cast<string>().OrderBy(f => f))
    {
        sb.AppendLine(key.PadLeft(40, ' ') + " : " + HttpContext.Current.Request.Cookies[key].Value + " - ExpireDate: " +  HttpContext.Current.Request.Cookies[key].Expires);
    }

    sb.AppendLine("");
    sb.AppendLine("---------------  Session ------------------------");
    foreach (string key in HttpContext.Current.Session.Keys.Cast<string>().OrderBy(f => f))
    {
        sb.AppendLine(key.PadLeft(40, ' ') + " : " + HttpContext.Current.Session[key]);
    }

    return sb.ToString();
}

 

The result looks like this::

-----------------------------------------  PostedParameters -----------------------------------------

-----------------------------------------  Query String ---------------------------------------------
                                username : ted@example.com

-----------------------------------------  Http Headers ---------------------------------------------
                                  Accept : text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
                          Accept-Charset : ISO-8859-1,utf-8;q=0.7,*;q=0.3
                         Accept-Encoding : gzip,deflate,sdch
                         Accept-Language : en-US,en;q=0.8
                           Cache-Control : max-age=0
                              Connection : keep-alive
                                  Cookie : timeOffset=-120; gs_u=1746198188:22843:26224:1320393450549; LANG=en; USER=a33f8…
                                    Host : dev.filemail.com
                              User-Agent : Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome..

-----------------------------------------  Cookies --------------------------------------------------
                                  __utma : 155161261.1943841533.1313506820.1324476330.1325070578.60 - ExpireDate: 01.01.0001 00:00:00
                                  __utma : 155161261.1943841533.1313506820.1324476330.1325070578.60 - ExpireDate: 01.01.0001 00:00:00
                                  __utmc : 260895642 - ExpireDate: 01.01.0001 00:00:00
                                  __utmz : 155161261.1313506820.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none) - ExpireDate:
                                  __utmz : 155161261.1313506820.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none) - ExpireDate:
                       ASP.NET_SessionId : a2xlln05ibzzd2ajywhpizc0 - ExpireDate: 01.01.0001 00:00:00
                                    gs_u : 1746198188:22843:26224:1320393450549 - ExpireDate: 01.01.0001 00:00:00
                                    LANG : en - ExpireDate: 01.01.0001 00:00:00
                              timeOffset : -120 - ExpireDate: 01.01.0001 00:00:00
                                    USER : a33f8a58568f430e8a0d48115ac7b13c - ExpireDate: 01.01.0001 00:00:00

-----------------------------------------  Session --------------------------------------------------
                                       - : -
                              UserObject :

 

by Njål

WebClient // HttpWebResponse – Problems with Chunked Transfer encoding

 

Doing a HTTP POST to a webserver using C# is pretty easy using the System.Net.HttpWebRequest. However – if you need to retrieve the answer/reply from the webserver – you’ll often get an exception:

Exception: 'Unable to read data from the transport connection: The connection was closed.'

When looking at the HttpWebResponse in the debugger – the length of the response is -1, so something strange is going on here.

When investigating further in the VS 2010 debugger/ Fiddler, I found out that the webserver (IIS 7 in this case) is sending the response using Transfer-Encoding: Chunked – which is relatively common in HTTP 1.1. It means that the length of the message isn’t specified in the HTTP Header Content-Length, but in the message itself.

Nothing wrong with this – but apparently HTTPWebResponse does not support Chunked Transfer Encoding (and thereby not HTTP 1.1).

The easiest way to fix this is to make sure the HttpWebRequest is sent using HTTP 1.0 – this way the server will reply with a HTTP Header specifying Content-Length.

HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("http://www.degree.no");
wr.ProtocolVersion = Version.Parse("1.0");

An easier/alternative way of doing HTTP Communication in the .NET framework is the System.Net.WebClient class. But since it uses HttpWebRequest under its hood,  the same problem occurs here.

There is no way (afaik) to specify WebClient to use HTTP 1.0 – so use HttpWebRequest with HTTP 1.0 if you need to POST something to a server and read the response.