攔截asp.net輸出流並進行處理的方法
阿新 • • 發佈:2018-12-30
本文例項主要實現對已經生成了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 }