如何使用 HttpModule 记录请求输入流,然后重置 InputStream 位置
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我已经解决了这个问题:我认为在 StreamReader 上调用 dispose 也一定会杀死 InputStream。
我没有使用 StreamReader,而是执行了以下操作:
所以完整的代码:
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:
So the complete code:
是的,StreamReader 将关闭提供的流。
如果您使用的是 >v4.5,请使用 StreamReader 构造函数使流保持打开状态。
Yes the StreamReader will close the supplied stream.
If you're on >v4.5, use a StreamReader constructor that leaves the stream open.
我不得不对“cbp”提供的答案进行一些小小的调整。当使用他的代码时我只得到零。我将位置设置为 0 以上,现在它可以工作了。
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.
这个答案不起作用。它返回一个包含空值的数组。
because the input stream consumed.
this answer did not work. it returns an array that contains null values.
because the input stream consumed.
您需要使用请求过滤器。编写一个派生自 Stream 的类并将其注册为一个过滤器。
You need to use a request filter. Write a class deriving from Stream and register it as a filter.
我只是将这一行放在使用部分的前面:
请求.InputStream.Position = 0;
让我很开心...
I just put this line in front of the using part:
Request.InputStream.Position = 0;
made my day...
有时,
RequestFilter
不会运行到方法 Read。 W3WP 似乎没有通过正常方式读取httprequest
的内容。如果您将
WEBservice
部署到服务器。然后使用 IHttpModule 来捕获它。添加RequestFilter
。但是 RequestFilter 的方法
Read()
不运行:Psometime,
RequestFilter
don't run to method Read. It seem be W3WP don't read content ofhttprequest
by normal way.If you deploy
WEbservice
to server. Then use IHttpModule for catch it. AddRequestFilter
.But method
Read()
of RequestFilter don't run :P