在ASP.NET中使用HTTP压缩
网友 博客园 http://www.cnblogs.com/libra163/
通过GZIP或DEFLATE方式对所有页面进行压缩,会大大缩小页面传输的容量。可以使用Fiddler2来观察使用压缩后页面大小的变化。
 
在项目中添加Global.asax,然添加如下代码:
      protected void Application_BeginRequest(object sender, EventArgs e)
      {
          HttpApplication app = (HttpApplication)sender;
          string acceptEncoding = app.Request.Headers["Accept-Encoding"];
          Stream prevUncompressedStream = app.Response.Filter;
          if (acceptEncoding == null || acceptEncoding.Length == 0) return;
          acceptEncoding = acceptEncoding.ToLower();
          if (acceptEncoding.Contains("gzip"))
          {
              // gzip
              app.Response.Filter = new GZipStream(prevUncompressedStream, CompressionMode.Compress);
              app.Response.AppendHeader("Content-Encoding", "gzip");
          }
          else if (acceptEncoding.Contains("deflate"))
          {
              // defalte
              app.Response.Filter = new DeflateStream(prevUncompressedStream, CompressionMode.Compress);
              app.Response.AppendHeader("Content-Encoding", "deflate");
          }
      }
 
CIO之家 www.ciozj.com 公众号:imciow
关联的文档
也许您喜欢