Asp.net2.0 中自定义过滤器对Response内容进行处理

1.首先要建立一个简易过滤器。
代码如下:
using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Text.RegularExpressions;
using  System.IO;
using  System.Web;

    
/**/ /// <summary>
    
/// 定义原始数据EventArgs,便于在截获完整数据后,由事件传递数据
    
/// </summary>

     public   class  RawDataEventArgs : EventArgs
    
{
        
private string sourceCode;

        
public RawDataEventArgs(string SourceCode)
        
{
            sourceCode 
= SourceCode;
        }

        
public string SourceCode
        
{
            
get return sourceCode; }
            
set { sourceCode = value; }
        }

    }


// 自定义过滤器
     public   class  RawFilter : Stream
    
{

        Stream responseStream;
        
long position;
        StringBuilder responseHtml;

        
/**//// <summary>
        
/// 当原始数据采集成功后激发。
        
/// </summary>

        public event EventHandler<RawDataEventArgs> OnRawDataRecordedEvent;

        
public RawFilter(Stream inputStream)
        
{
            responseStream 
= inputStream;
            responseHtml 
= new StringBuilder();
        }


//实现Stream 虚方法
        Filter OverridesFilter Overrides

//关键的点,在HttpResponse 输入内容的时候,一定会调用此方法输入数据,所以要在此方法内截获数据
        public override void Write(byte[] buffer, int offset, int count)
        
{
            
string strBuffer = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);

//采用正则,检查输入的是否有页面结束符</html>
            Regex eof = new Regex("</html>", RegexOptions.IgnoreCase);

            
if (!eof.IsMatch(strBuffer))
            
{
              
//页面没有输出完毕,继续追加内容
                responseHtml.Append(strBuffer);
            }

            
else
            
{
              
//页面输出已经完毕,截获内容
                responseHtml.Append(strBuffer);
                
string finalHtml = responseHtml.ToString();

            
//激发数据已经获取事件
            OnRawDataRecordedEvent(thisnew RawDataEventArgs(finalHtml));

                
//继续传递要发出的内容写入流
                byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(finalHtml);

                responseStream.Write(data, 
0, data.Length);
            }

        }

    }
 至此,过滤器定义完毕了,接下来还需要把这个过滤器装配到HttpResponse 对象中。
为了能够截获整站的aspx 页面输出的内容,我们可以定义一个HttpModule 来完成。
代码如下:
using  System;
using  System.Web;
using  System.Collections.Generic;
using  System.Text;
using  System.IO;
using  System.Diagnostics;

    
public   class  HttpRawDataModule : IHttpModule
    
{
        IHttpModule 成员
IHttpModule 成员

        
/**//// <summary>
        
/// 对此HTTP请求处理的过程全部结束
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        void context_ReleaseRequestState(object sender, EventArgs e)
        
{
            HttpApplication application 
= (HttpApplication)sender;

            
//这里需要针对ASPX页面进行拦截,测试发现如果不这么做,Wap 访问站点图片容易显示为X,奇怪
            string[] temp = application.Request.CurrentExecutionFilePath.Split('.');
            
if (temp.Length > 0 && temp[temp.Length - 1].ToLower() == "aspx")
            
{
                
//装配过滤器
                application.Response.Filter = new RawFilter(application.Response.Filter);

                
//绑定过滤器事件
                RawFilter filter = (RawFilter)application.Response.Filter;
                filter.OnRawDataRecordedEvent 
+= new EventHandler<RawDataEventArgs>(filter_OnRawDataRecordedEvent);
            }

        }


        
/**//// <summary>
        
/// 当原始数据采集到以后,入库 
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        void filter_OnRawDataRecordedEvent(object sender, RawDataEventArgs e)
        
{
            
string allcode = e.SourceCode;
            WapSite.SiteDataClass wapdata 
= new WapSite.SiteDataClass();
            wapdata.WriteRawDataLog(allcode);
        }

    }

HttpModule 准备完毕,也装配上了过滤器,接下来还需要在配置文件中配置HttpModules配置节 ,把自定义的HttpModule 加入到HTTP处理管道中。
在Web.config 中增加配置节如下:
< system.web >
    
< httpModules >
      
< add name = " RawDataModule "  type = " HttpRawDataModule " />
    
</ httpModules >
  
</ system.web >
测试成功,能准确的获得服务器向客户端输出的HTML内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值