午夜视频在线网站,日韩视频精品在线,中文字幕精品一区二区三区在线,在线播放精品,1024你懂我懂的旧版人,欧美日韩一级黄色片,一区二区三区在线观看视频

分享

截獲asp.net http輸出流自己做處理,替換html內(nèi)容_站長域名論壇www.91c...

 mcqlzz 2010-10-27

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原來的OutPut
        tw_new = new StringWriter(content);//一個StringWriter,用來獲取頁面內(nèi)容
        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);
    }

    protected override void Render(HtmlTextWriter writer)
    {
        base.Render(writer);
        //替換回Response的OutPut
        tw_field.SetValue(HttpContext.Current.Response, tw_old);
        //做自己的處理
        content.AppendLine("<!--江湖小子-->");
        HttpContext.Current.Response.Write(content.ToString());
    }
}

方法二,用HttpModul來實現(xiàn):

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>
///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原來的OutPut
        tw_new = new StringWriter(_content);//一個StringWriter,用來獲取頁面內(nèi)容
        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)
    {
        //替換回Response的OutPut
        tw_field.SetValue(HttpContext.Current.Response, tw_old);
        //做自己的處理
        _content.AppendLine("<!--江湖小子-->");
        HttpContext.Current.Response.Write(_content.ToString());
    }

}

方法三:
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);
        //做自己的處理
        _content.AppendLine("<!--jhxz-->");
        _contextApplication.Response.Write(_content.ToString());
    }

    public void Dispose()
    {
        _contextApplication = null;
        _contextApplication.Dispose();
    }
}

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多