博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
监控HTTP(1)
阅读量:6156 次
发布时间:2019-06-21

本文共 2405 字,大约阅读时间需要 8 分钟。

在配置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);

        }
    }

转载于:https://www.cnblogs.com/fujinliang/archive/2012/12/29/2839117.html

你可能感兴趣的文章
前端第七天
查看>>
图解SSH原理及两种登录方法
查看>>
【总结整理】JQuery基础学习---样式篇
查看>>
查询个人站点的文章、分类和标签查询
查看>>
基础知识:数字、字符串、列表 的类型及内置方法
查看>>
JSP的隐式对象
查看>>
JS图片跟着鼠标跑效果
查看>>
[SCOI2005][BZOJ 1084]最大子矩阵
查看>>
学习笔记之Data Visualization
查看>>
Leetcode 3. Longest Substring Without Repeating Characters
查看>>
416. Partition Equal Subset Sum
查看>>
app内部H5测试点总结
查看>>
[TC13761]Mutalisk
查看>>
while()
查看>>
常用限制input的方法
查看>>
IIS7下使用urlrewriter.dll配置
查看>>
并行程序设计学习心得1——并行计算机存储
查看>>
bulk
查看>>
C++ 迭代器运算
查看>>
【支持iOS11】UITableView左滑删除自定义 - 实现多选项并使用自定义图片
查看>>