声明为deflate的http请求不起作用

时间:2014-02-19 14:00:55

标签: c# http request deflate

我想发一个包含压缩有效载荷数据的HTTP请求。

HttpContent类的实现如下:

public class DeflateJsonContent<T> : HttpContent
{
    private readonly MemoryStream _sink = new MemoryStream();

    public DeflateJsonContent(T model)
    {
        Headers.ContentType = new MediaTypeHeaderValue("application/json");
        Headers.Add("Content-Encoding", "deflate");

        using(var deflate = new DeflateStream(_sink, CompressionMode.Compress, true))
        {
            Json.Serialize(deflate, model, true);
        }

        _sink.Seek(0, SeekOrigin.Begin);
    }

    protected override Task SerializeToStreamAsync(Stream destination, TransportContext context)
    {
        return _sink.CopyToAsync(destination);
    }

    protected override bool TryComputeLength(out long length)
    {
        length = _sink.Length;
        return true;
    }

    protected override void Dispose(bool disposing)
    {
        _sink.Dispose();
        base.Dispose(disposing);
    }
}

请求如下所示:

    private Task<HttpResponseMessage> PostDeflateJsonContentAsync(string uri, HttpContent content, CancellationToken token)
    {
        return _http.PostAsync(uri, content, token);
    }

使用Fiddler检查通信时,我发现请求标头中没有“deflate”。另外,Fiddler没有说请求有效负载是压缩的。

我错过了什么吗?

1 个答案:

答案 0 :(得分:-1)

您的客户端也需要发送正确的接受标头以进行放气;它不仅仅是服务器端的事情。请参阅维基百科页面:http://en.wikipedia.org/wiki/HTTP_compression