拦截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 }
方法的实现原理是:把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
- 拦截asp.net输出流并进行处理的方法
- 拦截asp.net输出流做处理
- 阻拦asp.net输出流并进行处置的办法
- 用asp.net调用excel并输出的一点经验
- asp.net错误处理最好的方法
- ASP.NET mvc异常处理的方法
- DataGrid输出Excel并进行格式化处理
- asp.net利用Ajax和Jquery在前台向后台传参数并返回值,通过aspx.cs中的静态方法+WebMethod进行处理
- ASP.NET: 拦截并分发运行时动态创建控件的事件
- ASP.NET中输出JS的两种方法
- ASP.Net 更新页面输出缓存的几种方法
- asp.net 不用控件 循环输出数据库数据的方法
- ASP.Net 更新页面输出缓存的几种方法
- asp获取网页源代码并处理乱码的方法
- asp.net C#进行网页间多值传递的方法
- discuz NT 与asp.net进行整合的方法
- 在 asp.net 下进行 CKEditor 3.2 配置的方法
- 在ASP.NET中进行文件处理
- 求正整数n所有可能的和式的组合。
- 大数 实现一个加法器,使其能够输出a+b的值。 注意进位
- 面试题 倒转字符串
- 向一个table中插入tr
- C获取稳健大小
- 拦截asp.net输出流并进行处理的方法
- 嵌入式 创建一个2G的空文件(Linux命令dd)
- 输入两个多项式,计算它们的和。
- C# GDI+技术
- Android手机 Fildder真机抓包
- ISP、ICP、IAP三种编程方式区别总结
- Chromium Graphics: 3D上下文及其虚拟化(Virtualization) - Part II
- 矩阵旋转90度
- 简述configure、pkg-config、pkg_config_path三者的关系