自定义httpmodule里面:
public virtual void Init(HttpApplication app)
{
// WARNING! This does not work with Windows authentication!
// If you are using Windows authentication, change to app.BeginRequest
app.BeginRequest += (sender, e) =>
{
app.Context.Response.Filter =
new CatchTextStream(app.Response.Filter);
};
}
public class CatchTextStream : Stream
{
private Stream output;
public CatchTextStream(Stream s)
{
output = s;
}
public override bool CanRead
{
get { return output.CanRead; }
}
public override bool CanSeek
{
get { return output.CanSeek; }
}
public override bool CanWrite
{
get { return output.CanWrite; }
}
public override void Flush()
{
output.Flush();
}
public override long Length
{
get { return output.Length; }
}
public override long Position
{
get { return output.Position; }
set { output.Position = value; }
}
public override int Read(byte[] buffer, int offset, int count)
{
return output.Read(buffer, offset, count);
}
public override long Seek(long offset, SeekOrigin origin)
{
return output.Seek(offset, origin);
}
public override void SetLength(long value)
{
output.SetLength(value);
}
public override void Write(byte[] buffer, int offset, int count)
{
StringComparison ignore = StringComparison.CurrentCultureIgnoreCase;
if (HttpContext.Current != null)
{
HttpContext context = HttpContext.Current;
if (context.Response.ContentType.Equals("text/html", ignore))
{
Encoding encoding = context.Response.ContentEncoding;
//此处获取返回的HTML内容
string html = encoding.GetString(buffer, offset, count);
byte[] bytes = encoding.GetBytes(html);
output.Write(bytes, 0, bytes.Length);
else
output.Write(buffer, offset, count);
}
}
}