//GZIP压缩
//查看请求头部
string acceptEncoding = context.Request.Headers["Accept-Encoding"].ToString().ToUpperInvariant();
if (!String.IsNullOrEmpty(acceptEncoding))


{
//如果头部里有包含"GZIP”,"DEFLATE",表示你浏览器支持GZIP,DEFLATE压缩
if (acceptEncoding.Contains("GZIP"))

{
//向输出流头部添加压缩信息
context.Response.AppendHeader("Content-encoding", "gzip");
context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress);
}
else if (acceptEncoding.Contains("DEFLATE"))

{
//向输出流头部添加压缩信息
context.Response.AppendHeader("Content-encoding", "deflate");
context.Response.Filter = new DeflateStream(context.Response.Filter, CompressionMode.Compress);
}
}
这样每次context.Response.Write出支的数据就压缩了
对效多的文本信息压缩可以压缩到原来三分之一到四分之一的样子
如果发送的信息只有几个字节就没有必要了
下面来看两次压缩的对比
上面这个是压缩前的
下面这个是压缩后的
来比较一下.
压缩前
第一个请求:3021字节,用时0.033
第二个请求: 431字节,用时0.010
第三个请求: 516字节,用时0.008
压缩后
第一个请求:1239字节,用时0.025
第二个请求: 556字节,用时0.008
第三个请求: 587字节,用时0.008
第一个请求压缩很明显,达到了原来的三分之一左右
第二个请求,第三个请求压缩前反而比压缩后小.这是为什么呢.
呵.那是第一个请求发送的数据本来就很大,
第二个和第三个请求发送的就只有几个字节,
还要加上浏览器的一些信息,可能反而大了吧
我的理解是这样的.还请高手多多指教

本作品采用知识共享署名-非商业性使用-相同方式共享 3.0 Unported许可协议
欢迎转载,但必须保留文章的署名刘巨(艾梦尔)
并保留此链接:http://liuju150.cnblogs.com/
如有疑问请发邮件:liuju150@vip.qq.com
Tag标签: JSON,asp.net,.net,ashx,gzip,deflate