如何使用 HttpModule 记录请求输入流,然后重置 InputStream 位置

发布于 2024-08-10 17:52:27 字数 732 浏览 3 评论 0原文

我正在尝试使用 IHttpModule 来记录 http 请求的内容,如下所示:

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

问题是,在读取输入流末尾后,InputStream 似乎消失了,或更可能的是,光标位于溪流。

我尝试过 request.InputStream.Position = 0;request.InputStream.Seek(0, SeekOrigin.Begin); 但都不起作用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

淡笑忘祈一世凡恋 2024-08-17 17:52:27

我已经解决了这个问题:我认为在 StreamReader 上调用 dispose 也一定会杀死 InputStream。

我没有使用 StreamReader,而是执行了以下操作:

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

所以完整的代码:

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

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)
    }
}
或十年 2024-08-17 17:52:27

是的,StreamReader 将关闭提供的流。

如果您使用的是 >v4.5,请使用 StreamReader 构造函数使流保持打开状态。

using (var reader = new StreamReader(request.InputStream, Encoding.UTF8, true, 1024, true))
{
    content = reader.ReadToEnd();
}

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();
}
月竹挽风 2024-08-17 17:52:27

我不得不对“cbp”提供的答案进行一些小小的调整。当使用他的代码时我只得到零。我将位置设置为 0 以上,现在它可以工作了。

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

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);
帅气称霸 2024-08-17 17:52:27

这个答案不起作用。它返回一个包含空值的数组。

        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.

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.

再见回来 2024-08-17 17:52:27

您需要使用请求过滤器。编写一个派生自 Stream 的类并将其注册为一个过滤器。

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

看轻我的陪伴 2024-08-17 17:52:27

我只是将这一行放在使用部分的前面:
请求.InputStream.Position = 0;

让我很开心...

I just put this line in front of the using part:
Request.InputStream.Position = 0;

made my day...

那一片橙海, 2024-08-17 17:52:27

有时,RequestFilter 不会运行到方法 Read。 W3WP 似乎没有通过正常方式读取 httprequest 的内容。

如果您将WEBservice部署到服务器。然后使用 IHttpModule 来捕获它。添加RequestFilter

但是 RequestFilter 的方法 Read() 不运行:P

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文