How to log request inputstream with HttpModule, then reset InputStream position
Asked Answered
D

6

33

I am trying to log the contents of an http request, using an IHttpModule like so:

public class LoggingModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += ContextBeginRequest;
    }

    private void ContextBeginRequest(object sender, EventArgs e)
    {
        var request = ((HttpApplication)sender).Request;
        string content;

        using (var reader = new StreamReader(request.InputStream))
        {
            content = reader.ReadToEnd();
        }

        LogRequest(content)
    }
}

The problem is that after reading the input stream to the end, the InputStream seems to have either disappeared or more likely, the cursor is at the end of the stream.

I have tried request.InputStream.Position = 0; and request.InputStream.Seek(0, SeekOrigin.Begin); but neither work.

Duggins answered 5/11, 2009 at 7:12 Comment(0)
D
48

I've worked out the problem: I think that calling dispose on the StreamReader must be killing the InputStream too.

Instead of using the StreamReader I did the following:

        var bytes = new byte[request.InputStream.Length];
        request.InputStream.Read(bytes, 0, bytes.Length);
        request.InputStream.Position = 0;
        string content = Encoding.ASCII.GetString(bytes);

So the complete code:

public class LoggingModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += ContextBeginRequest;
    }

    private void ContextBeginRequest(object sender, EventArgs e)
    {
        var request = ((HttpApplication)sender).Request;

        var bytes = new byte[request.InputStream.Length];
        request.InputStream.Read(bytes, 0, bytes.Length);
        request.InputStream.Position = 0;
        string content = Encoding.ASCII.GetString(bytes);

        LogRequest(content)
    }
}
Duggins answered 5/11, 2009 at 7:25 Comment(2)
Be careful with the encoding there, I think you'll want it to be Encoding.UTF8.GetString(bytes);Damaris
I'd wasted a whole day befor I got what's wrong with my code. Thanks you a lot!Kristiankristiansand
S
23

Yes the StreamReader will close the supplied stream.

If you're on >v4.5, use a StreamReader constructor that leaves the stream open.

using (var reader = new StreamReader(request.InputStream, Encoding.UTF8, true, 1024, true))
{
    content = reader.ReadToEnd();
}
Syblesybley answered 29/11, 2013 at 1:50 Comment(0)
N
3

I had to make a small tweak to the answer provided by "cbp". When using his code I just got zeros. I moved setting the position to 0 above the read and now it works.

 var bytes = new byte[Request.InputStream.Length];
 Request.InputStream.Position = 0;
 Request.InputStream.Read(bytes, 0, bytes.Length);
 string content = Encoding.ASCII.GetString(bytes);
Namnama answered 10/3, 2016 at 17:39 Comment(0)
S
2

this answer did not work. it returns an array that contains null values.

        var bytes = new byte[request.InputStream.Length];
        request.InputStream.Read(bytes, 0, bytes.Length);
        request.InputStream.Position = 0;
        string content = Encoding.ASCII.GetString(bytes);

because the input stream consumed.
Stephanestephani answered 26/1, 2010 at 15:28 Comment(0)
H
1

You need to use a request filter. Write a class deriving from Stream and register it as a filter.

Hooks answered 5/11, 2009 at 7:20 Comment(1)
Please elaborate how this is superior to other answers? Does it not require resetting the stream?Comestible
H
-1

sometime, RequestFilter don't run to method Read. It seem be W3WP don't read content of httprequest by normal way.

If you deploy WEbservice to server. Then use IHttpModule for catch it. Add RequestFilter.

But method Read() of RequestFilter don't run :P

Hazan answered 27/6, 2012 at 7:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.