我尝试使用 GZipStream 在页面上强制进行 gzip 压缩,但浏览器显示我正在使用不受支持的压缩

发布于 2024-09-13 06:53:47 字数 1323 浏览 2 评论 0原文

我正在尝试实现 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 while
CompressionUtils.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦初启 2024-09-20 06:53:47

所以事实证明我绑定得太晚了,绑定到了 PostMapRequestHandler 而不是 PreSendRequestHeaders。现在工作正常。

So it turns out I was just binding too late, bound to PostMapRequestHandler instead of PreSendRequestHeaders. Working fine now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文