从IHttpModule派生后,我编写了一个 HTTPModule ,如下所示。
public void Init(HttpApplication httpApplication)
{
EventHandlerTaskAsyncHelper taskAsyncHelper = new EventHandlerTaskAsyncHelper(LogMessage);
httpApplication.AddOnBeginRequestAsync(taskAsyncHelper.BeginEventHandler, taskAsyncHelper.EndEventHandler);
}
private async Task LogMessage(object sender, EventArgs e)
{
var app = (HttpApplication)sender;
var ctx = app.Context;
string myURL = ((HttpApplication)sender).Context.Request.Url.ToString();
StreamReader reader = new StreamReader(((HttpApplication)sender).Context.Request.InputStream);
try
{
string body = reader.ReadToEnd();
}
finally
{
reader.BaseStream.Position = 0;
}
StreamReader ResponseStreamReader = new StreamReader(((HttpApplication)sender).Context.Response.OutputStream);
}
StreamReader 代码抛出错误(确切的错误消息是:System.ArgumentException: Stream was not readable)。
如何读取 HTTP 请求的 ResponseBody。我正在使用.Net 4.5。谢谢你,约翰