如何在Asp.Net 4.0/IIS7中支持压缩的HTTP请求?

Joa*_*rel 8 asp.net iis gzip request http-compression

对于ASP.NET 4.0/IIS7 Web应用程序,我想支持压缩的HTTP 请求.基本上,我想支持将添加Content-Encoding: gzip到请求标头中的客户端,并相应地压缩主体.

有谁知道我是如何实现这种行为的?

Ps:关于,我有多个端点REST和SOAP,它感觉更好的解决方案来支持HTTP级别的压缩,而不是每个端点的自定义编码器.

Joa*_*rel 5

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()
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

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.

  • 经典的HTTP压缩仅适用于**响应**,这是**请求**我试图压缩. (2认同)