拦截asp.net输出流并进行处理的方法

来源:互联网 发布:sohu的yum源 编辑:程序博客网 时间:2024/05/17 21:42
本文实例主要实现对已经生成了HTML的页面做一些输出到客户端之前的处理。


方法的实现原理是:把Response的输出重定e78.com向到自定义的容器内,也就是我们的StringBuilder对象里,在HTML所有的向页面输出都变成了向StringBuilder输出,然后我们对StringBuilder处理完成之后,再把Response的输出重定向到原来的页面上,然后再通过Response.Write方法把StringBuilder的内容输出到页面上。


这里之所以用反射,是因为Response对象的OutPut属性是只读的,通过反编译该类的程序集发现,OutPut实际上是内部私有成员 _writer来实现输出的。因此通过反射来改写该成员的值以实现输出流的重定向。


具体功能代码如下:

view sourceprint?01 using System;   


02 using System.Collections.Generic;   


03 using System.Linq;   


04 using System.Web;   


05 using System.Web.UI;   


06 using System.Web.UI.WebControls;   


07 using System.Text;   


08 using System.IO;   


09 using System.Reflection;   


10 public partial class _Default : System.Web.UI.Page    


11 {   


12   StringBuilder content = new StringBuilder();   


13   TextWriter tw_old, tw_new;   


14   FieldInfo tw_field;   


15   protected void Page_Load(object sender, EventArgs e)   


16   {   


17     var context = HttpContext.Current;   


18     


19     tw_old = context.Response.Output;//Response原来的OutPut   


20     tw_new = new StringWriter(content);//一个StringWriter,用来获取页面内容   


21     var type_rp = context.Response.GetType();   


22     //通过反射获取对象的私有字段   


23     tw_field = type_rp.GetField("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);   


24     tw_field.SetValue(context.Response, tw_new);   


25   }   


26   protected override void Render(HtmlTextWriter writer)   


27   {   


28     base.Render(writer);   


29     //替换回Response的OutPut   


30     tw_field.SetValue(HttpContext.Current.Response, tw_old);   


31     //做自己的处理   


32     content.AppendLine("<!--江湖小子-->");   


33     HttpContext.Current.Response.Write(content.ToString());   


34   }   


35 }   


方法二,用HttpModul来实现:  




view sourceprint?01 using System;   


02 using System.Collections.Generic;   


03 using System.Linq;   


04 using System.Web;   


05 using System.Web.UI;   


06 using System.IO;   


07 using System.Text;   


08 using System.Reflection;   


09 /// <summary>   


10 ///HttpModule 的摘要说明   


11 /// </summary>   


12 public class HttpModule : IHttpModule   


13 {   


14   private HttpApplication _contextApplication;   


15   private TextWriter tw_new, tw_old;   


16   private StringBuilder _content;   


17   private FieldInfo tw_field;   


18   public void Init(HttpApplication context)   


19   {   


20     _contextApplication = context;   


21     _contextApplication.PreRequestHandlerExecute += new EventHandler(_contextApplication_PreRequestHandlerExecute);   


22   }   


23   public void Dispose()   


24   {   


25     _contextApplication = null;   


26     _contextApplication.Dispose();   


27   }   


28   public void _contextApplication_PreRequestHandlerExecute(object sender, EventArgs e)   


29   {   


30     HttpContext context = _contextApplication.Context;   


31     


32     var _page = context.Handler as System.Web.UI.Page;   


33     _page.Unload += new EventHandler(_page_Unload);   


34     


35     _content = new StringBuilder();   


36     tw_old = context.Response.Output;//Response原来的OutPut   


37     tw_new = new StringWriter(_content);//一个StringWriter,用来获取页面内容   


38     var type_rp = context.Response.GetType();   


39     tw_field = type_rp.GetField("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);   


40     tw_field.SetValue(context.Response, tw_new);   


41   }   


42   void _page_Unload(object sender, EventArgs e)   


43   {   


44     //替换回Response的OutPut   


45     tw_field.SetValue(HttpContext.Current.Response, tw_old);   


46     //做自己的处理   


47     _content.AppendLine("<!--江湖小子-->");   


48     HttpContext.Current.Response.Write(_content.ToString());   


49   }   


50 }   


方法三:


view sourceprint?01 public class HttpModule : IHttpModule   


02 {   


03   private HttpApplication _contextApplication;   


04   private TextWriter tw_new, tw_old;   


05   private StringBuilder _content;   


06   private FieldInfo tw_field;   


07   public void Init(HttpApplication application)   


08   {   


09     _contextApplication = application;   


10     _contextApplication.BeginRequest += new EventHandler(_contextApplication_BeginRequest);   


11     _contextApplication.EndRequest +=new EventHandler(_contextApplication_EndRequest);   


12   }   


13   void _contextApplication_BeginRequest(object sender, EventArgs e)   


14   {   


15     _content = new StringBuilder();   


16     tw_old = _contextApplication.Response.Output;   


17     tw_new = new StringWriter(_content);   


18     var type_rp = _contextApplication.Response.GetType();   


19     tw_field = type_rp.GetField("_writer", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);   


20     tw_field.SetValue(_contextApplication.Response, tw_new);   


21   }   


22   void _contextApplication_EndRequest(object sender, EventArgs e)   


23   {   


24     tw_field.SetValue(_contextApplication.Response, tw_old);   


25     //做自己的处理   


26     _content.AppendLine("<!--jhxz-->");   


27     _contextApplication.Response.Write(_content.ToString());   


28   }   


29   public void Dispose()   


30   {   


31     _contextApplication = null;   


32     _contextApplication.Dispose();   


33   }   


34 } 
0 0
原创粉丝点击