I have written a HTTPModule after deriving from IHttpModule as follows.
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);
}
The StreamReader code is throwing error(the exact error message is: System.ArgumentException: Stream was not readable).
How can I read the ResponseBody of a HTTP request. I am using .Net 4.5. Thank you, John
The OutputStream represents the output as it is being written, so you may need to set the reader position to 0 before attempting to read from it.