欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)頁(yè)面GZip或Deflate壓縮的方法

 更新時(shí)間:2015年06月09日 16:51:01   作者:McJeremy&Fan  
這篇文章主要介紹了C#實(shí)現(xiàn)頁(yè)面GZip或Deflate壓縮的方法,涉及C#通過(guò)GZipStream與DeflateStream實(shí)現(xiàn)頁(yè)面壓縮的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了C#實(shí)現(xiàn)頁(yè)面GZip或Deflate壓縮的方法。分享給大家供大家參考。具體分析如下:

.NET Framework里
System.IO.Compression下有兩個(gè)可用于頁(yè)面壓縮的類,GZipStream和 DeflateStream.

在頁(yè)面被傳輸之前,需要獲取發(fā)出請(qǐng)求的客戶端所采用的解碼形式。
可以通過(guò)Request.Headers["Accept-Encoding"]來(lái)獲取。

在頁(yè)面被壓縮之前,需要獲取頁(yè)面實(shí)體主體,可通過(guò)
Response.Filter來(lái)獲?。⊿tream類型)

示例代碼:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.IO.Compression;
/// <summary>
/// GzipDeflate 的摘要說(shuō)明
/// </summary>
public class GzipDeflate:IHttpModule
{
  public GzipDeflate()
  {
    //
    // TODO: 在此處添加構(gòu)造函數(shù)邏輯
    //
  }
  public void Init(HttpApplication app)
  {
    app.BeginRequest += new EventHandler(app_BeginRequest);
  }
  void app_BeginRequest(object sender, EventArgs e)
  {
    //HTTP頭域可分為四類:通用頭、請(qǐng)求頭、響應(yīng)頭、實(shí)體頭。
    HttpApplication app=(HttpApplication)sender;
    string acceptEncoding = app.Request.Headers["Accept-Encoding"];
    //客戶端支持的解碼方式。屬于請(qǐng)求頭。
    Stream requestStream = app.Response.Filter;
    acceptEncoding = acceptEncoding.ToLower();
    if(acceptEncoding.Contains("gzip"))
    {
      app.Response.Filter = new GZipStream(requestStream, CompressionMode.Compress);
      app.Response.AppendHeader("Content-Encoding", "gzip");
    }
    else if(acceptEncoding.Contains("deflate"))
    {
      app.Response.Filter = new DeflateStream(requestStream, CompressionMode.Compress);
      app.Response.AppendHeader("Content-Encoding", "deflate");
      //屬于實(shí)體頭。
    }
  }
  public void Dispose()
  {
  }
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論