在配置IHttpModule的实现类可以通过:
HttpRequest:
得到HTTP请求的正文信息:
request.InputStream.Position = 0;//设置流的位置
StreamReader reader = new StreamReader(request.InputStream);//request请求流
string aa = reader.ReadToEnd();
request.InputStream.Seek(0, SeekOrigin.End);//还原到原来的位置
得到请求的头部信息:
HttpRequest request = m_application.Context.Request;
string head = string.Empty;
for (int i = 0; i < request.Headers.Count; i++)
{
head += request.Headers.GetKey(i) + ":" + request.Headers[i]+"\n";
}
HttpResponse的response.OutputStream流不能读只能写,但其提供了一个过滤器要使用前需要声明:
HttpApplication application = sender as HttpApplication;
string[] temp = application.Request.CurrentExecutionFilePath.Split('.');//虚拟路径
if (temp.Length > 0)
{
application.Response.Filter = new RawFilter(application.Response.Filter);
RawFilter filter = (RawFilter)application.Response.Filter;
filter.OnRawDataRecordedEvent += new EventHandler(filter_OnRawDataRecordedEvent);
}
public class RawDataEventArgs : EventArgs
{
private string sourceCode;
public RawDataEventArgs(string SourceCode)
{
this.sourceCode = SourceCode;
}
public string SourceCode
{
get { return sourceCode; }
set { sourceCode = value; }
}
}
//自定义过滤器
public class RawFilter : Stream
{
Stream responseStream;
long position;
StringBuilder responseHtml;
/*当原始数据采集成功后激发*/
public event EventHandler OnRawDataRecordedEvent;
public RawFilter(Stream inputStream)
{
responseStream = inputStream;
responseHtml = new StringBuilder();
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return true; }
}
public override bool CanWrite
{
get { return true; }
}
public override void Flush()
{
responseStream.Flush();
}
public override long Length
{
get { return 0; }
}
public override long Position
{
get
{
return position;
}
set
{
position = value;
}
}
public override int Read(byte[] buffer, int offset, int count)
{
return responseStream.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
return responseStream.Seek(offset, origin);
}
public override void SetLength(long value)
{
responseStream.SetLength(value);
}
/*关键的点,在HttpResponse 输入内容的时候,一定会调用此方法输入数据,所以要在此方法内截获数据,经测试能获取webservice时的xml*/
public override void Write(byte[] buffer, int offset, int count)
{
string aa = System.Text.UTF8Encoding.UTF8.GetString(buffer);
responseStream.Write(buffer, offset, count);
}
}