2010-06-24 63 views

回答

1

不幸的是,HttpResponse.OutputStream只寫,所以這是不是非常簡單 - 任何嘗試一下輸出流的Length屬性將拋出異常。

我見過的唯一解決方案是對Response對象應用過濾器,以便過濾器可以計算字節數。

快速谷歌搜索landed me here,這似乎接近我記得的實現。

1

希望它幫助

context.PostRequestHandlerExecute += delegate(object sender, EventArgs e) 
{ 
    HttpContext httpContext = ((HttpApplication)sender).Context; 
    HttpResponse response = httpContext.Response; 
    // Don't interfere with non-HTML responses 
    if (response.ContentType == "text/html") 
    { 
     response.Filter = new MyRewriterStream(response.Filter); 
    } 
}; 

MyRewriterStream類

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Web; 

namespace ExecutionTime 
{ 
    public class MyRewriterStream:Stream 
    { 
     #region "Propiedades" 

     private Stream _sink; 

     #endregion 
     public MyRewriterStream(System.IO.Stream stream) 
     { 

      _sink = stream; 
     } 
     public override void Write(byte[] buffer, int offset, int count) 
     { 
      string outStr; 
      outStr = UTF8Encoding.UTF8.GetString(buffer, offset, count); 
      strPageSize = strPageSize + outStr; 
      StringBuilder sb = new StringBuilder(outStr); 

      if (sb.ToString().LastIndexOf("</html>") > 0) 
      { 
       string HtmlResponse = "";//HERE PUT YOUR NEW HTML RESPONSE 
       sb.AppendLine(HtmlResponse); 
       byteArray = Encoding.ASCII.GetBytes(sb.ToString()); 
       _sink.Write(byteArray, 0, byteArray.Length); 
      } 
      else 
      { 
       _sink.Write(buffer, offset, count); 
      } 

     } 

     public override void Flush() 
     { 
      _sink.Flush(); 
     } 


     #region Properites 

     public override bool CanRead 
     { 
      get { return true; } 
     } 

     public override bool CanSeek 
     { 
      get { return true; } 
     } 

     public override bool CanWrite 
     { 
      get { return true; } 
     } 

     //public override void Flush() 
     //{ 
     // _sink.Flush(); 
     //} 

     public override long Length 
     { 
      get { return 0; } 
     } 

     private long _position; 
     public override long Position 
     { 
      get { return _position; } 
      set { _position = value; } 
     } 

     #endregion 

     #region Methods 

     public override int Read(byte[] buffer, int offset, int count) 
     { 
      return _sink.Read(buffer, offset, count); 
     } 

     public override long Seek(long offset, SeekOrigin origin) 
     { 
      return _sink.Seek(offset, origin); 
     } 

     public override void SetLength(long value) 
     { 
      _sink.SetLength(value); 
     } 

     public override void Close() 
     { 
      _sink.Close(); 
     } 
     #endregion 
    } 
}