How to support compressed HTTP requests in Asp.Net 4.0 / IIS7?

前端 未结 3 2060
萌比男神i
萌比男神i 2021-02-15 14:37

For an ASP.NET 4.0 / IIS7 web app, I would like to support compressed HTTP requests. Basically, I would like to support clients that would add Content-Enc

3条回答
  •  渐次进展
    2021-02-15 15:12

    Although hacky, you can get around WCF using the original Content-Length even after the request has been decompressed by setting the private _contentLength field in the HttpRequest class using reflection. Using Joannes Vermorel's code:

        void BeginRequest(object sender, EventArgs e)
        {
            var app = (HttpApplication)sender;
    
            if ("gzip" == app.Request.Headers["Content-Encoding"])
            {
                app.Request.Filter = new GZipStream(
                    app.Request.Filter, CompressionMode.Decompress);
    
                // set private _contentLength field with new content length after the request has been decompressed
                var contentLengthProperty = typeof(HttpRequest).GetField("_contentLength", BindingFlags.NonPublic | BindingFlags.Instance);
                contentLengthProperty.SetValue(app.Request, (Int32)app.Request.InputStream.Length);
            }
        }
    

提交回复
热议问题