2012-02-17 23 views
2

我已經創建了一個ASP.net應用程序與幾個aspx。其中一些像API鏈接一樣使用。這些API頁面被J2ME應用程序使用,它傳遞一些頭文件。我正在特別檢查這些標題「Accept-Encoding」,以便我的頁面可以發送壓縮內容。如何發送在asp.net中的壓縮數據

以下代碼顯示了其中一個API頁面的示例代碼。

byte[] buffer; 
    int bufferLength; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     bufferLength = Request.ContentLength; 
     buffer = new byte[bufferLength]; 
     buffer = Request.BinaryRead(bufferLength); 
     string s = Encoding.ASCII.GetString(buffer); 

     bool needEncrypted = (Request.Headers["Accept-Encoding"] != null); 

     if (!string.IsNullOrEmpty(s)) 
     { 
      JavaScriptSerializer ser = new JavaScriptSerializer(); 
      CRequestQuestParameter QuestPara = ser.Deserialize<CRequestQuestParameter>(s); 
      CUtil utilFun = new CUtil(WebConfigurationManager.ConnectionStrings["TheConnectionString"].ToString()); 

      CResponseQuestionSets res = new CResponseQuestionSets(); 


      //No error setting 
      res.ErrorCode = -1; 
      res.ErrorMessage = ""; 
      res.IsError = false; 

      //Return data 
      res.ResponseData = utilFun.GetQuestionSet(QuestPara); 
      if (res.ResponseData != null) { 
       res.ErrorCode = -1; 
       res.ErrorMessage = ""; 
       res.IsError = false; 

      } 
      else 
      { 
       res.ErrorCode = 102; 
       res.ErrorMessage = "User does not exists"; 
       res.IsError = true; 

      } 



      if (needEncrypted) 
      { 
       HttpContext context = HttpContext.Current; 
       context.Response.Filter = new GZipStream(context.Response.Filter, CompressionMode.Compress); 
       HttpContext.Current.Response.AppendHeader("Content-encoding", "gzip"); 
      } 

      Response.ContentType = "application/json charset=utf-8"; 
      Response.Write(ser.Serialize(res)); 
      Response.End(); 

     } 
    } 

的這裏的問題是,J2ME應用程序不接收數據的壓縮,但確實得到頭「內容編碼」。

任何人都可以幫我解決這個問題嗎?

+0

也許如果你在這次調用的開始時移動過濾器,我的意思是在頁面init。 – Aristos 2012-02-17 12:21:46

+0

@Aristos:好的,會檢查。 – IrfanRaza 2012-02-17 12:24:36

回答

2

您不需要在代碼中執行GZIP編碼。

這完全通過在IIS 6IIS 7中正確配置您的站點來處理。