您的位置:首页 > 理论基础 > 计算机网络

httpmodule通过httpfilter获取返回的网页内容

2014-07-17 19:15 435 查看
自定义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);

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: