如何检测我的HTTP模块中是否为特定请求启用了GZIP?我对输出应用了一个过滤器,当它对gzip压缩的内容起作用时,它会以某种方式搞砸压缩,并且客户端浏览器会抛出一个无法解码内容的错误。
public void Init(HttpApplication context)
{
// if(HttpContext.Current.IsCompressed) // Check for compressed content here
// Set up the filter / replacement.
context.PostReleaseRequestState += (_, __) =>
{
var filterStream = new ResponseFilterStream(HttpContext.Current.Response.Filter);
filterStream.TransformString += FilterPage;
HttpContext.Current.Response.Filter = filterStream;
};
}
ResponseFilterStream
是一个自定义流,它缓存所有流写入并将内容显示为事件,以允许方法修改流的内容。它非常适合修改HTML请求(这是我想要的),但我不希望它对gzip压缩的响应起作用。如何检测gzip压缩响应并阻止过滤器流连接到响应?
答案 0 :(得分:3)
要获得回复,您可以检查Encoding
http标头的值gzip
或deflate
对于请求,您需要检查Accept-Encoding
http标头,以获取gzip
或deflate
编码的值。