簡體   English   中英

告訴RestSharp *不*添加特定的HTTP標頭

[英]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發送這些請求以查看正在發生什么時,我遇到的問題是我的請求突然得到了第三個不需要的HTTP標頭:

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標頭(並且我不想在其中包含)。 現在我的響應不再是正確的JSON(我可以解析)了,但是突然我取回了壓縮的二進制數據(在嘗試JSON反序列化時效果不佳)。

如何擺脫第三個不需要的HTTP標頭?

  • 我嘗試將其設置為其他內容-輸入的內容只會附加到這些設置中
  • 我試圖以某種方式“清除” HTTP標頭-沒有成功
  • 我嘗試在RestClient或RestRequest類上查找一個屬性以指定“不使用GZip”

查看RestSharp的源( Http.Sync.csHttp.Async.cs ),您可以看到這些值是硬編碼的:

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

還有一個開放式問題描述此問題。 它於2014年8月開放,但仍未解決。 我認為您可以在此處發表評論,也許他們會關注。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM