我正在尝试使用RestSharp从C#ASP.NET 4.0应用程序调用REST服务。
对https://地址进行相当简单的POST调用;我的代码是这样的(CheckStatusRequest
是一个普通的简单DTO,大约有四个或五个string
和int
属性 - 没什么特别的):
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(我能够解析),但突然之间我又回到了gzip压缩二进制数据(当尝试JSON反序列化时,它并没有做得很好)...
如何摆脱第三个不需要的HTTP标头?
答案 0 :(得分:2)
查看RestSharp的来源(Http.Sync.cs和Http.Async.cs),您可以看到这些值是硬编码的:
webRequest.AutomaticDecompression =
DecompressionMethods.Deflate | DecompressionMethods.GZip | DecompressionMethods.None;
还有一个开放的issue来描述这个问题。它于2014年8月开放,但仍未解决。我想你可以在那里发表评论,也许他们会关注。