我正在使用 RestSharp调用 REST 服务。我的电话看起来像这样:
var request = new RestRequest("/foo", Method.POST);
request.JsonSerializer.ContentType = "application/json; charset=utf-8";
request.AddJsonBody(new string[] { "param1", "param2" });
var response = this._client.Execute<Foo>(request);
对于大多数其他调用,这工作正常。当响应被压缩时,我遇到了问题。响应中的 header (大部分)看起来像这样:
HTTP/1.1 200 OK
Uncompressed-Size: 35000
Content-Length: 3019
Content-Encoding: deflate
Content-Type: application/json
问题是当我使用 RestSharp 调用此方法时,我不断收到错误消息:
Error: Block length does not match with its complement.
我已尝试在请求中设置 Accept-Encoding
header ,但它仍然会产生错误。我也尝试使用自定义反序列化器,但错误发生在反序列化之前。据我所知,如果 Content-Encoding
header 显示 deflate
(确实如此),RestSharp 应该自动处理通缩。
如何让 RestSharp 正确处理通货紧缩?
更新
最后,我能够更改服务以在请求中查找值为 identity
的 Accept-Encoding
header 。如果找到,则更改服务以返回未压缩的数据。
不幸的是,这并不是原始问题的真正解决方案,但它确实解决了我的问题。如果发布了更好的解决方案,我会尝试。
最佳答案
根据这篇文章,如果您不在内容类型中传递 charset=utf-8,您应该能够处理它。
请引用: RestSharp compress request while making rest call to server
关于c# - 如何让 RestSharp 正确缩小压缩的 HTTP 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32149746/