HTTP Compression - 速用方法

本文介绍了一种简单的HTTP压缩实现方案,通过检测浏览器支持的压缩算法(如GZip或Deflate),并在服务器响应中使用相应的压缩算法来减少传输的数据量。此外,还提供了针对Konqueror浏览器已知问题的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HTTP Compression Made Easy

All the modern browsers support HTTP Compression. I tested this solution with Microsoft Internet Explorer 6 SP1, Mozilla Firefox 1.5.0.4, and Konqueror. The latter has a few (known) problems with GZip streams: the pages are displayed correctly, but a warning message is shown.

When the browser supports HTTP Compression, it sends to the server a specific request header:

Accept-encoding: gzip, deflate

When the server receives that header, it can enable HTTP compression. The server, if it decides to use HTTP Compression, must notify the browser using the response header:

Content-encoding: gzip
-- or --
Content-encoding: deflate

The server then appends the content compressed using the GZip or Deflate algorithm. Note: there are a few other compression algorithms, but they are not supported by all browsers. For further information.

 

The Code

The code is unbelievably simple, and uses the property Filter of the Page object. This property allows you to perform operations on the output stream of the page, for example, replacing every occurrence of :) with the appropriate HTML tag, just to display the smiley (I use this method in my blog). We only have to "replace" the whole output with its compressed bit stream. All we need is to put this simple code snippet in the Page_Load method:

 

protected void Page_Load(object sender, EventArgs e) {
if(Request.Headers["Accept-encoding"] != null &&
Request.Headers["Accept-encoding"].Contains("gzip")) {
Response.Filter = new GZipStream(Response.Filter,
CompressionMode.Compress, true);
Response.AppendHeader("Content-encoding", "gzip");
}
else if(Request.Headers["Accept-encoding"] != null &&
Request.Headers["Accept-encoding"].Contains("deflate")) {
Response.Filter = new DeflateStream(Response.Filter,
CompressionMode.Compress, true);
Response.AppendHeader("Content-encoding", "deflate");
}
}

The code "plugs" a GZipStream or a DeflateStream into the Filter object (it's an instance of System.IO.Stream), trying to detect which algorithm the browser supports. Note: the two classes (GZipStream and DeflateStream) use the same compression algorithm.

 

Refinements

Since it seems that Konqueror has problems with GZip/Deflate, we should disable HTTP Compression for that browser. We can do that by checking the User Agent. The code then becomes:

protected void Page_Load(object sender, EventArgs e) {
  if(!Request.UserAgent.ToLower().Contains("konqueror")) {
    if(Request.Headers["Accept-encoding"] != null &&
       Request.Headers["Accept-encoding"].Contains("gzip")) {
          Response.Filter = new GZipStream(Response.Filter,
                            CompressionMode.Compress, true);
          Response.AppendHeader("Content-encoding", "gzip");
    }
    else if(Request.Headers["Accept-encoding"] != null &&
            Request.Headers["Accept-encoding"].Contains("deflate")) {
          Response.Filter = new DeflateStream(Response.Filter,
                            CompressionMode.Compress, true);
          Response.AppendHeader("Content-encoding", "deflate");
    }
  }
}

 

Conclusions and Disclaimer

As the article's title states, this is a quick and dirty solution, with all its problems, bugs, and worst-practices. It should be considered a mere proof-of-concept, although it actually works. In the coming weeks, I'll investigate deeply into this topic because I believe it may actually become a nice way to implement HTTP Compression on a per-page basis.

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值