截取HttpResponse输出流(转)

截取HttpResponse输出流(转)

出处:http://www.cnblogs.com/mxw09/archive/2010/12/15/1906783.html

 
传统方法如下:

 

protected override void Render(HtmlTextWriter writer) {           
string content = string.Empty;             StringWriter stringWriter = new StringWriter();             HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);             try             {                 // 将当前页面的内容呈现到临时的 HtmlTextWriter 对象中                  base.Render(htmlWriter);                 htmlWriter.Close();                 // 得到当前页面的全部内容                  content = stringWriter.ToString();                 // 替换页面中的部分内容                  string newContent = content.Replace("[mxh]", "孟宪会");                 // 将新页面的内容显示出来                  writer.Write(newContent);             }             catch { }             finally             {                 stringWriter.Dispose();                 htmlWriter.Close();                 htmlWriter.Dispose();             }
}

 

//原创方法如下:这个方法与在render方法里自己实例化一个HtmlTextWriter ,然后调用base.Render的时候传入自己的实例化的HtmlTextWriter 然后获取输出html的区别是:前者能够截获在aspx页面中通过Response.Write的字符串并且顺序正确,后者在Render方法里的字符串content不能截取页面中通过Response.Write的字符串所以会导致输出以后html内容顺序错乱。

protected override void Render(HtmlTextWriter writer)         {           

base.Render(writer);
            //Response.Output其实就是一个HttpWriter,Response.OutputStream其实就是HttpResponseStream,Object.ReferenceEquals (Response.Output,(Response.OutputStream as HttpResponseStream)._writer)为true
            BindingFlags bind = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.GetField;             //因为HttpWriter._charBuffer这个字符数组的长度是1024,所以推测,一旦某一段字符串超过1024就会创建一个IHttpResponseElement,然后加入到HttpWriter._buffers,HttpWriter._buffers的每一个元素都实现了IHttpResponseElement接口,具体类型可能是HttpResponseUnmanagedBufferElement,HttpSubstBlockResponseElement等类型             ArrayList arr = (ArrayList)Response.Output.GetType().GetField("_buffers", bind).GetValue(Response.Output);
            Assembly systemWeb = Assembly.Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");             Type type = systemWeb.GetType("System.Web.IHttpResponseElement");             MethodInfo method = type.GetMethod("GetBytes");             StringBuilder sb = new StringBuilder(5000);             //遍历每一个buffer,获取buffer里存储的字符数组,然后转换为字符串             for (int i = 0; i < arr.Count; i++)             {                 byte[] buffer = (byte[])method.Invoke(arr[i], null);                 //使用当前编码得出已经存储到HttpWriter._buffers中的字符串                 sb.Append(Response.ContentEncoding.GetString(buffer));             }             //获取HttpWriter的字符数组缓冲区             char[] charBuffer = (char[])Response.Output.GetType().GetField("_charBuffer", bind).GetValue(Response.Output);             int charBufferLength = (int)Response.Output.GetType().GetField("_charBufferLength", bind).GetValue(Response.Output);             int charBufferFree = (int)Response.Output.GetType().GetField("_charBufferFree", bind).GetValue(Response.Output);             //charBufferLength - charBufferFree 等于字符数组缓冲区已经使用的字符数量             for (int i = 0; i < charBufferLength - charBufferFree; i++)             {                 sb.Append(charBuffer[i]);             }
        }
posted @ 2014-05-27 13:18  snowalkmount  阅读(635)  评论(0编辑  收藏  举报