locked
HttpModule To Copy Response to database RRS feed

  • Question

  • User-725046759 posted
    Hi,

    I have implemented a Service Class which is derived from IHttpModule, but I'm having some trouble gettng the information received saved to the database.

    What I need is to get the page response into a db table. Basically, if I submit a request of http://localhost/blah.aspx?x=1&y=2
    I expect to return:

    <response>
      <x>1</x>
      <y>2</y>
    </response>
    
    

    I want the request and this response logged to the database, and finally, the response to display the above to the client as though nothing had happened.
    My codes so far as below:

    namespace MyNS
    {
        public class MyModule : IHttpModule
        {
            public delegate void MyEventHandler(Object s, EventArgs e);
            private MyEventHandler _eventHandler = null;
            public event MyEventHandler MyEvent
            {
                add { _eventHandler += value; }
                remove { _eventHandler -= value; }
            }
    
            static Object m_LockObject = new Object();
            public MyModule()
            { }
            public void Init(HttpApplication objApplication)
            {
                objApplication.BeginRequest += new EventHandler(this.ContextBeginRequest);
            }
            public void Dispose()
            {
            }
    
            public void ContextBeginRequest(object sender, EventArgs e)
            {
                try
                {
                    lock (m_LockObject)
                    {
                        using (StreamWriter sw = new StreamWriter(@"C:\requestLog.txt", true))
                        {
                            //Begin Request
                            sw.WriteLine("Begin request called at " + DateTime.Now.ToString() + "; URL: " + HttpContext.Current.Request.RawUrl.ToString());
                            
                            //End Request
                            sw.WriteLine("End request called at " + DateTime.Now.ToString() + "; URL: " + HttpContext.Current.Request.RawUrl.ToString());
                            sw.WriteLine(String.Empty);
                            sw.Close();
    
                            HttpApplication app = sender as HttpApplication;
                            BufferedStream buff = new BufferedStream(app.Response.Filter);
                            CopyStream(app.Request.InputStream, buff);
                            MyUtility.WriteToDB(DateTime.Now, DateTime.Now, "request", buff.Length.ToString());
    
                            if (_eventHandler != null)
                                _eventHandler(this, null);
                        }
                    }
                }
                catch (Exception ex)
                {
                }
            }
    
            public static void CopyStream(Stream input, Stream output)
            {
                byte[] buffer = new byte[32768];
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    output.Write(buffer, 0, read);
                }
            }
        }
    }

    Help in any way truly appreciated!
    Wednesday, January 4, 2012 3:39 AM

Answers