Popular Posts
Enable SSL connection for Jsoup import org.jsoup.Connection; import org.jsoup.Jsoup; import javax.net.ssl.*; import java.io.IOException; import java.security.KeyManagement... Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i... Change the AppDomain's Base Directory and Environment Directory // Update AppDomain's Base Directory string root_path = "c:\\temp\\"; AppDomain.CurrentDomain.SetData("APPBASE", roo...
Stats
Capture response output stream using HttpModule
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace TestWebApplication
{
    public class ResponseSniffModule : IHttpModule
    {
        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += context_PreRequestHandlerExecute;
            context.PreSendRequestContent += context_PreSendRequestContent;
        }

        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            var application = sender as HttpApplication;
            var context = application.Context;
            var response = context.Response;

            // Add a filter to capture response stream
            response.Filter = new ResponseSniffer(response.Filter);
        }

        void context_PreSendRequestContent(object sender, EventArgs e)
        {
            var application = sender as HttpApplication;
            var context = application.Context;
            var response = context.Response;

            var filter = response.Filter as ResponseSniffer;

            if (filter != null)
            {
                var reader = new StreamReader(new MemoryStream(filter.RecordStream.GetBuffer()));
                var source = reader.ReadToEnd();
            }
        }
    }

    public class ResponseSniffer : Stream
    {
        private readonly Stream ResponseStream;

        public MemoryStream RecordStream { get; set; }

        #region Implements of Stream
        public override bool CanRead
        {
            get { return ResponseStream.CanRead; }
        }

        public override bool CanSeek
        {
            get { return ResponseStream.CanSeek; }
        }

        public override bool CanWrite
        {
            get { return ResponseStream.CanWrite; }
        }

        public override void Flush()
        {
            ResponseStream.Flush();
        }

        public override long Length
        {
            get { return ResponseStream.Length; }
        }

        public override long Position
        {
            get
            {
                return ResponseStream.Position;
            }
            set
            {
                ResponseStream.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 Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            ResponseStream.SetLength(value);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            RecordStream.Write(buffer, offset, count);
            ResponseStream.Write(buffer, offset, count);
        }
        #endregion

        public ResponseSniffer(Stream stream)
        {
            RecordStream = new MemoryStream();
            ResponseStream = stream;
        }
    }
}