Search code examples
c#asp.netihttpmodule

Logging request length using IHttpModule


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);
    }

Solution

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