0

I've made an IHttpModule that logs web requests. It logs POST requests just fine. However for GET requests, the length always seems to be zero. Is there a way to get the length of the GET request?

    public void Init(HttpApplication context)
    {
        context.BeginRequest += ContextBeginRequest;
    }

    private void ContextBeginRequest(object sender, EventArgs e)
    {
            string url = app.Request.RequestContext.HttpContext.Request.Url.ToString();
            string requestType = app.Request.RequestContext.HttpContext.Request.RequestType;
            long requestLength = app.Request.InputStream.Length;
            Log(url, requestType, requestLength);
    }
Fidel
  • 7,027
  • 11
  • 57
  • 81

1 Answers1

2

GET request usually don't have a message body and, thus, the Content-Length will be zero.

Rodrick Chapman
  • 5,437
  • 2
  • 31
  • 32
  • Thanks Rodrick, but how do I get the length of the entire request? (all the HTTP header stuff) – Fidel Jun 23 '14 at 06:35
  • @Fidel I can't test this now, but you could try iterating over the server variables directly. I'd start with ALL_RAW. – Rodrick Chapman Jun 23 '14 at 06:56
  • Thanks Rodrick, yep ServerVariables["ALL_RAW"] gets pretty damn close. However it doesn't contain the first line of the HTTP request. – Fidel Jun 23 '14 at 07:19
  • 2
    @Fidel: First line is simply `GET HTTP/1.1` (the verb can be get from Request.HttpMethod and the path/query from Request.Url.PathAndQuery) – jgauffin Jun 23 '14 at 07:37