0

I am having an issue about .net HttpModule. I create a HttpModule to capture website's request, it can capture all the websites page except the ajax request.

Here is my web.config. Moduler.FilterModuler is what I created.thanks.

   `<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>`

here is my module.

    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
        //
    }
}

}`

Chris Cheung
  • 79
  • 1
  • 16
  • More information is needed. How is the code of your HttpModule? What event have you tried to intercept? "HttpApplication.PostRequestHandlerExecute", "HttpApplication.PostAcquireRequestState", "HttpApplication.PostMapRequestHandler", etc? – Hailton Jul 16 '12 at 22:47
  • Hi @Hailton, the httpmodule was attached.thanks. – Chris Cheung Jul 16 '12 at 23:21

1 Answers1

0

I use this example to solve a similar problem could not capture the HttpModule events solicudes AJAX

HttpModule to Handle Ajax Calls
I hope you functions

  • thx david, it looks like something else in my web.config capture the ajax request, but I try to removed other sections in web.config, the ajax request still couldnot fire the events. – Chris Cheung Jul 16 '12 at 23:25