压缩 HTTP GET 响应



我目前正在将我的一些MVC3控制器迁移到MVC4 API控制器。我已经实现了 MVC3 控制器的压缩机制 通过固有ActionFilterAttribute和覆盖方法获取方法响应OnActionExecutiong。经过一番研究,我发现我需要使用System.Web.HttpFiltersActionFilterMethod。如果有人可以分享一段示例代码来让我开始使用 GZip

最简单的方法是直接在 IIS 级别启用压缩。

如果要在应用程序级别执行此操作,可以编写一个自定义委派消息处理程序,如以下帖子所示:

public class CompressHandler : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        return base.SendAsync(request, cancellationToken).ContinueWith<HttpResponseMessage>((responseToCompleteTask) =>
        {
            HttpResponseMessage response = responseToCompleteTask.Result;
            if (response.RequestMessage.Headers.AcceptEncoding != null)
            {
                string encodingType = response.RequestMessage.Headers.AcceptEncoding.First().Value;
                response.Content = new CompressedContent(response.Content, encodingType);
            }
            return response;
        },
        TaskContinuationOptions.OnlyOnRanToCompletion);
    }
}
public class CompressedContent : HttpContent
{
    private HttpContent originalContent;
    private string encodingType;
    public CompressedContent(HttpContent content, string encodingType)
    {
        if (content == null)
        {
            throw new ArgumentNullException("content");
        }
        if (encodingType == null)
        {
            throw new ArgumentNullException("encodingType");
        }
        originalContent = content;
        this.encodingType = encodingType.ToLowerInvariant();
        if (this.encodingType != "gzip" && this.encodingType != "deflate")
        {
            throw new InvalidOperationException(string.Format("Encoding '{0}' is not supported. Only supports gzip or deflate encoding.", this.encodingType));
        }
        // copy the headers from the original content
        foreach (KeyValuePair<string, IEnumerable<string>> header in originalContent.Headers)
        {
            this.Headers.AddWithoutValidation(header.Key, header.Value);
        }
        this.Headers.ContentEncoding.Add(encodingType);
    }
    protected override bool TryComputeLength(out long length)
    {
        length = -1;
        return false;
    }
    protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
    {
        Stream compressedStream = null;
        if (encodingType == "gzip")
        {
            compressedStream = new GZipStream(stream, CompressionMode.Compress, leaveOpen: true);
        }
        else if (encodingType == "deflate")
        {
            compressedStream = new DeflateStream(stream, CompressionMode.Compress, leaveOpen: true);
        }
        return originalContent.CopyToAsync(compressedStream).ContinueWith(tsk =>
        {
            if (compressedStream != null)
            {
                compressedStream.Dispose();
            }
        });
    }
}

现在剩下的就是在Application_Start中注册处理程序:

GlobalConfiguration.Configuration.MessageHandlers.Add(new CompressHandler());

如果您使用的是IIS 7+,我会说将压缩留给IIS,因为它支持GZIP压缩。只需将其打开即可。

另一方面,压缩对于控制器来说太靠近金属。理想情况下,控制器应该在比字节和流高得多的级别上工作。

使用类并编写以下代码

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class CompressFilter : ActionFilterAttribute
{
    public override void OnActionExecuted(HttpActionExecutedContext context)
    {
        var acceptedEncoding = context.Response.RequestMessage.Headers.AcceptEncoding.First().Value;
        if (!acceptedEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase)
        && !acceptedEncoding.Equals("deflate", StringComparison.InvariantCultureIgnoreCase))
        {
            return;
        }
        context.Response.Content = new CompressedContent(context.Response.Content, acceptedEncoding);
    }
}

现在创建另一个类并编写以下代码。

public class CompressedContent : HttpContent
{
    private readonly string _encodingType;
    private readonly HttpContent _originalContent;
    public CompressedContent(HttpContent content, string encodingType = "gzip")
    {
        if (content == null)
        {
            throw new ArgumentNullException("content");
        }
        _originalContent = content;
        _encodingType = encodingType.ToLowerInvariant();
        foreach (var header in _originalContent.Headers)
        {
            Headers.TryAddWithoutValidation(header.Key, header.Value);
        }
        Headers.ContentEncoding.Add(encodingType);
    }
    protected override bool TryComputeLength(out long length)
    {
        length = -1;
        return false;
    }
    protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
    {
        Stream compressedStream = null;
        switch (_encodingType)
        {
            case "gzip":
                compressedStream = new GZipStream(stream, CompressionMode.Compress, true);
                break;
            case "deflate":
                compressedStream = new DeflateStream(stream, CompressionMode.Compress, true);
                break;
            default:
                compressedStream = stream;
                break;
        }
        return _originalContent.CopyToAsync(compressedStream).ContinueWith(tsk =>
        {
            if (compressedStream != null)
            {
                compressedStream.Dispose();
            }
        });
    }
}

现在在控制器或任何像这样的 api 操作方法中使用以下属性

[Route("GetData")]
[CompressFilter]         
public HttpResponseMessage GetData()
{
}

最新更新