告诉 RestSharp *不* 添加特定的 HTTP header

Telling RestSharp *not* to add a specific HTTP header

我正在尝试使用 RestSharp 从 C# ASP.NET 4.0 应用程序调用 REST 服务。

这是对 https:// 地址的相当简单的 POST 调用;我的代码是这样的(CheckStatusRequest 是一个简单的 DTO,大约有四个或五个 stringint 属性——没什么特别的):

public CheckStatusResponse CheckStatus(CheckStatusRequest request) {
    // set up RestClient
    RestClient client = new RestClient();
    string uri = "https://.......";

    // create the request (see below)
    IRestRequest restRequest = CreateRequestWithHeaders(url, Method.POST);

    // add the body to the request
    restRequest.AddBody(request);

    // execute call
    var restResponse = _restClient.Execute<CheckStatusResponse>(restRequest);
}   

// set up request
private IRestRequest CreateRequestWithHeaders(string uri, Method method) {
    // define request
    RestRequest request = new RestRequest(uri, method);

    // add two required HTTP headers
    request.AddHeader("Accept", "application/json");
    request.AddHeader("Content-Type", "application/json");

    // define JSON as my format
    request.RequestFormat = DataFormat.Json;

    // attach the JSON.NET serializer for RestSharp
    request.JsonSerializer = new RestSharpJsonNetSerializer();

    return request;
}

当我通过 Fiddler 发送这些请求以查看发生了什么时遇到的问题是我的请求突然得到 third 和不需要的 HTTP header:

POST https://-some-url- HTTP/1.1
Accept: application/json
User-Agent: RestSharp/104.4.0.0
Content-Type: application/json
Host: **********.com
Content-Length: 226
Accept-Encoding: gzip, deflate   <<<=== This one here is UNWANTED!
Connection: Keep-Alive

我突然有了那个 Accept-Encoding HTTP header,我从来没有指定它(我不想把它放在那里)。现在我的响应不再正确 JSON(我能够解析),但突然我取回了 gzip 二进制数据(在尝试 JSON-deserialize 时效果不佳)。 ...

如何摆脱第三个不需要的 HTTP header?

查看 RestSharp 的来源 (Http.Sync.cs and Http.Async.cs),您可以看到这些值是硬编码的:

webRequest.AutomaticDecompression = 
    DecompressionMethods.Deflate | DecompressionMethods.GZip | DecompressionMethods.None;

还有一个开放的issue描述了这个问题。它于 2014 年 8 月开放,但仍未解决。我想你可以在那里发表评论,也许他们会关注。