Can gzip compression be selectively disabled in ASP.NET/IIS 7?

前端 未结 3 820
长情又很酷
长情又很酷 2020-12-02 15:17

I am using a long-lived asynchronous HTTP connection to send progress updates to a client via AJAX. When compression is enabled, the updates are not received in discrete ch

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 15:34

    What about you set the gzip compression by your self, selectivle when you wish for ? On the Application_BeginRequest check when you like to make and when you dont make compression. Here is a sample code.

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
        string cTheFile = HttpContext.Current.Request.Path;
        string sExtentionOfThisFile = System.IO.Path.GetExtension(cTheFile);
    
        if (sExtentionOfThisFile.Equals(".aspx", StringComparison.InvariantCultureIgnoreCase))
        {
            string acceptEncoding = MyCurrentContent.Request.Headers["Accept-Encoding"].ToLower();;
    
            if (acceptEncoding.Contains("deflate") || acceptEncoding == "*")
            {
                // defalte
                HttpContext.Current.Response.Filter = new DeflateStream(prevUncompressedStream,
                    CompressionMode.Compress);
                HttpContext.Current.Response.AppendHeader("Content-Encoding", "deflate");
            } else if (acceptEncoding.Contains("gzip"))
            {
                // gzip
                HttpContext.Current.Response.Filter = new GZipStream(prevUncompressedStream,
                    CompressionMode.Compress);
                HttpContext.Current.Response.AppendHeader("Content-Encoding", "gzip");
            }       
        }
    }
    

提交回复
热议问题