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

前端 未结 3 2071
萌比男神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 14:54

    For those who might be interested, the implementation is rather straightforward with an IHttpModule that simply filters incoming requests.

    public class GZipDecompressModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.BeginRequest += BeginRequest;
        }
    
        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);
            }
        }
    
        public void Dispose()
        {
        }
    }
    

    Update: It appears that this approach trigger a problem in WCF, as WCF relies on the original Content-Length and not the value obtained after decompressing.

提交回复
热议问题