Intercept asp.net output stream for processing and intercept asp.net output stream
The title of this article refers to the processing of HTML pages that have been generated before being output to the client.
The principle of the method is: redirect the Response output to the custom container, that is, in our StringBuilder object, all HTML output to the page is output to StringBuilder, after processing StringBuilder, redirect the Response output to the original page, and then use Response. write method to output the content of StringBuilder to the page
Reflection is used here because the OutPut attribute of the Response object is read-only. By decompiling the assembly of this class, it is found that OutPut is actually implemented by internal private member _ writer. Therefore, the value of this Member is rewritten through reflection to redirect the output stream.
[C-sharp]View plaincopy
- Using System;
- Using System. Collections. Generic;
- Using System. Linq;
- Using System. Web;
- Using System. Web. UI;
- Using System. Web. UI. WebControls;
- Using System. Text;
- Using System. IO;
- Using System. Reflection;
- Public partial class _ Default: System. Web. UI. Page
- {
- StringBuilder content = new StringBuilder ();
- TextWriter tw_old, tw_new;
- FieldInfo tw_field;
- Protected void Page_Load (object sender, EventArgs e)
- {
- Var context = HttpContext. Current;
- Tw_old = context. Response. Output; // Response Original OutPut
- Tw_new = new StringWriter (content); // A StringWriter, used to obtain page content
- Var type_rp = context. Response. GetType ();
- // Obtain the private field of the object through reflection
- Tw_field = type_rp.GetField ("_ writer", System. Reflection. BindingFlags. Public | System. Reflection. BindingFlags. NonPublic | System. Reflection. BindingFlags. Instance );
- Tw_field.SetValue (context. Response, tw_new );
- }
- Protected override void Render (HtmlTextWriter writer)
- {
- Base. Render (writer );
- // Replace the OutPut of Response
- Tw_field.SetValue (HttpContext. Current. Response, tw_old );
- // Perform your own Processing
- Content. AppendLine ("<! -- Jianghu kiddies --> ");
- HttpContext. Current. Response. Write (content. ToString ());
- }
- }
- Method 2: Use HttpModul to implement:
- Using System;
- Using System. Collections. Generic;
- Using System. Linq;
- Using System. Web;
- Using System. Web. UI;
- Using System. IO;
- Using System. Text;
- Using System. Reflection;
- /// <Summary>
- /// Summary of HttpModule
- /// </Summary>
- Public class HttpModule: IHttpModule
- {
- Private HttpApplication _ contextApplication;
- Private TextWriter tw_new, tw_old;
- Private StringBuilder _ content;
- Private FieldInfo tw_field;
- Public void Init (HttpApplication context)
- {
- _ ContextApplication = context;
- _ ContextApplication. PreRequestHandlerExecute + = new EventHandler (_ contextApplication_PreRequestHandlerExecute );
- }
- Public void Dispose ()
- {
- _ ContextApplication = null;
- _ ContextApplication. Dispose ();
- }
- Public void _ contextApplication_PreRequestHandlerExecute (object sender, EventArgs e)
- {
- HttpContext context = _ contextApplication. Context;
- Var _ page = context. Handler as System. Web. UI. Page;
- _ Page. Unload + = new EventHandler (_ page_Unload );
- _ Content = new StringBuilder ();
- Tw_old = context. Response. Output; // Response Original OutPut
- Tw_new = new StringWriter (_ content); // A StringWriter, used to obtain page content
- Var type_rp = context. Response. GetType ();
- Tw_field = type_rp.GetField ("_ writer", System. Reflection. BindingFlags. Public | System. Reflection. BindingFlags. NonPublic | System. Reflection. BindingFlags. Instance );
- Tw_field.SetValue (context. Response, tw_new );
- }
- Void _ page_Unload (object sender, EventArgs e)
- {
- // Replace the OutPut of Response
- Tw_field.SetValue (HttpContext. Current. Response, tw_old );
- // Perform your own Processing
- _ Content. AppendLine ("<! -- Jianghu kiddies --> ");
- HttpContext. Current. Response. Write (_ content. ToString ());
- }
- }
- Method 3:
- Public class HttpModule: IHttpModule
- {
- Private HttpApplication _ contextApplication;
- Private TextWriter tw_new, tw_old;
- Private StringBuilder _ content;
- Private FieldInfo tw_field;
- Public void Init (HttpApplication application)
- {
- _ ContextApplication = application;
- _ ContextApplication. BeginRequest + = new EventHandler (_ contextApplication_BeginRequest );
- _ ContextApplication. EndRequest + = new EventHandler (_ contextApplication_EndRequest );
- }
- Void _ contextApplication_BeginRequest (object sender, EventArgs e)
- {
- _ Content = new StringBuilder ();
- Tw_old = _ contextApplication. Response. Output;
- Tw_new = new StringWriter (_ content );
- Var type_rp = _ contextApplication. Response. GetType ();
- Tw_field = type_rp.GetField ("_ writer", System. Reflection. BindingFlags. Public | System. Reflection. BindingFlags. NonPublic | System. Reflection. BindingFlags. Instance );
- Tw_field.SetValue (_ contextApplication. Response, tw_new );
- }
- Void _ contextApplication_EndRequest (object sender, EventArgs e)
- {
- Tw_field.SetValue (_ contextApplication. Response, tw_old );
- // Perform your own Processing
- _ Content. AppendLine ("<! -- Jhxz --> ");
- _ ContextApplication. Response. Write (_ content. ToString ());
- }
- Public void Dispose ()
- {
- _ ContextApplication = null;
- _ ContextApplication. Dispose ();
- }
- }
Finally, I would like to recommend a good article: an episode on a business trip in Europe
Aspnet respone output stream problems
System. IO. StringWriter sw = new System. IO. StringWriter ();
HtmlTextWriter hw = new HtmlTextWriter (sw );
Datagrid. RenderControl (hw );
Response. Clear ();
Response. Write (sw );
Response. End ();
Asp net asynchronous calling of output streams
First, you write
OBao. open ("Post", "ao. aspx", false );
The last parameter "false" indicates synchronous operation. "true" indicates asynchronous operation.
If (! IsPostBack)
{
Response. Write ("testajax ");
Response. End ();
}
In this way, only testajax can be obtained.