我遇到关于.net HttpModule的问题。 我创建了一个HttpModule来捕获网站的请求,它可以捕获除ajax请求之外的所有网站页面。
这是我的web.config。 Moduler.FilterModuler就是我创建的。谢谢。
`<httpModules>
<add name="handleTimeOut" type="Moduler.FilterModuler"/>
</httpModules>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<modules>
<remove name="ScriptModule"/>
<add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<remove name="ScriptHandlerFactory"/>
<remove name="ScriptHandlerFactoryAppServices"/>
<remove name="ScriptResource"/>
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptResource" verb="GET,HEAD" path="ScriptResource.axd" preCondition="integratedMode" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</handlers></system.webServer>`
这是我的模块。
namespace Moduler
{
public class FilterModuler : IHttpModule, IReadOnlySessionState
{
private class ResponseCaptureStream : Stream
{
private readonly Stream _streamToCapture;
private readonly Encoding _responseEncoding;
private string _streamContent;
public string StreamContent
{
get { return _streamContent; }
private set
{
_streamContent = value;
}
}
public ResponseCaptureStream(Stream streamToCapture, Encoding responseEncoding)
{
_responseEncoding = responseEncoding;
_streamToCapture = streamToCapture;
}
public override bool CanRead
{
get { return _streamToCapture.CanRead; }
}
public override bool CanSeek
{
get { return _streamToCapture.CanSeek; }
}
public override bool CanWrite
{
get { return _streamToCapture.CanWrite; }
}
public override void Flush()
{
_streamToCapture.Flush();
}
public override long Length
{
get { return _streamToCapture.Length; }
}
public override long Position
{
get
{
return _streamToCapture.Position;
}
set
{
_streamToCapture.Position = value;
}
}
public override int Read(byte[] buffer, int offset, int count)
{
return _streamToCapture.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
return _streamToCapture.Seek(offset, origin);
}
public override void SetLength(long value)
{
_streamToCapture.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
string str = _responseEncoding.GetString(buffer, offset, count);
_streamContent += str;
}
public override void Close()
{
_streamContent = _streamContent.Replace("$Social Work$", "hello world");
byte[] data = _responseEncoding.GetBytes(_streamContent);
_streamToCapture.Write(data, 0, data.Length);
_streamToCapture.Close();
base.Close();
}
}
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest +=new EventHandler(context_BeginRequest);
context.ReleaseRequestState += new EventHandler(context_ReleaseRequestState);
}
protected void context_ReleaseRequestState(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
string[] temp = application.Request.CurrentExecutionFilePath.Split('.');
if (temp.Length > 0 && temp[temp.Length - 1].ToLower() == "aspx")
{
application.Response.Filter = new ResponseCaptureStream(application.Response.Filter, application.Response.ContentEncoding);
}
}
protected void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
using (StreamWriter sw = new StreamWriter("C:\\work\\request.txt"))
{
sw.Write(application.Request.CurrentExecutionFilePath);
}
}
public FilterModuler()
{
//
// TODO: Add constructor logic here
//
}
}
}`