我尝试使用 GZipStream 在页面上强制进行 gzip 压缩,但浏览器显示我正在使用不受支持的压缩
我正在尝试实现 Steve Souders 讨论的内容 http ://www.stevesouders.com/blog/2010/07/12/velocity-forcing-gzip-compression/关于强制gzip压缩
我有一个正在运行这个的模块:
void context_PreSendRequestHeaders(object sender, EventArgs e)
{
var app = sender as HttpApplication;
var request = app.Request;
var response = app.Response;
if (CompressionUtils.GzipSupported(request) || CompressionUtils.GzipNotSupportedExplicitly(request))
{
return;
}
if (CompressionUtils.GzipSupportedExplicitly(request))
{
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
response.AddHeader(HttpHeaderKey.ContentEncoding, "gzip");
return;
}
response.Write("<iframe style=\"display:none;\" src=\"/CompressedPage.aspx\"></iframe>");
}
CompressionUtils.GzipSupported< /code> 仅检查“accepts-encoding”标头 CompressionUtils.GzipSupportedExplicitly 和 CompressionUtils.GzipNotSupportedExplicitly 检查 cookie,说明浏览器是否真的可以读取 gzip
但是当我在 Firefox 中加载页面时,出现此错误:
内容编码错误
无法显示您尝试查看的页面,因为它使用了无效或 不支持的压缩形式。
在 Fiddler 中,它显示内容编码标头已添加,但内容尚未压缩
I'm tring to implement what Steve Souders discusses http://www.stevesouders.com/blog/2010/07/12/velocity-forcing-gzip-compression/ about forcing gzip compression
I've got a module that's running this:
void context_PreSendRequestHeaders(object sender, EventArgs e)
{
var app = sender as HttpApplication;
var request = app.Request;
var response = app.Response;
if (CompressionUtils.GzipSupported(request) || CompressionUtils.GzipNotSupportedExplicitly(request))
{
return;
}
if (CompressionUtils.GzipSupportedExplicitly(request))
{
response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
response.AddHeader(HttpHeaderKey.ContentEncoding, "gzip");
return;
}
response.Write("<iframe style=\"display:none;\" src=\"/CompressedPage.aspx\"></iframe>");
}
CompressionUtils.GzipSupported
just checks for the 'accepts-encoding' header whileCompressionUtils.GzipSupportedExplicitly
and CompressionUtils.GzipNotSupportedExplicitly
check for the cookie saying whether the browser really can read gzip
But when I load a page in Firefox I get this error:
Content Encoding Error
The page you are trying to view cannot be shown because it uses an invalid or
unsupported form of compression.
and in Fiddler it shows that the content-encoding header has been added but the content hasn't been compressed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
所以事实证明我绑定得太晚了,绑定到了 PostMapRequestHandler 而不是 PreSendRequestHeaders。现在工作正常。
So it turns out I was just binding too late, bound to
PostMapRequestHandler
instead ofPreSendRequestHeaders
. Working fine now.