使用RestSharp发布GZip内容 [英] Posting GZip content using RestSharp
问题描述
如何使用RestSharp发布GZip数据.我有以下代码,但无法正常运行:
How do I post GZip data using RestSharp. I have the following code but it isn't working as I would expect:
var restRequest = new RestRequest(url, Method.POST)
{
Timeout = Constants.DefaultTimeoutMilliseconds
};
var dataStream = new MemoryStream();
using (var zipStream = new GZipStream(dataStream, CompressionMode.Compress))
{
using (var writer = new StreamWriter(zipStream))
{
writer.Write(new DotNetXmlSerializer().Serialize(content));
}
}
var compressedBytes = dataStream.ToArray();
restRequest.AddParameter("application/x-gzip", compressedBytes, ParameterType.RequestBody);
return _restClient.Execute<TResponseData>(restRequest);
运行此命令并检查wireshark跟踪时,compressedBytes变量将发布为 'System.Byte []'-就像已调用ToString()一样,尽管参数是system.object.
When I run this and check the wireshark trace, the compressedBytes variable is posted as 'System.Byte[]' - as if ToString() has been called on it despite the parameter being a system.object.
如果我同时使用Convert.ToBase64String()和Encoding.Utf8.GetString()将压缩的字节数组作为字符串传递,那么我将无法在服务器上解压缩GZip.我只是得到'System.IO.InvalidDataException:GZip标头中的幻数不正确.确保您正在传递GZip'.
If I pass the compressed byte array through as a string using both Convert.ToBase64String() and Encoding.Utf8.GetString() then I am unable to decompress the GZip at the server. I simply get 'System.IO.InvalidDataException: The magic number in GZip header is not correct. Make sure you are passing in a GZip'.
是否可以使用RestSharp发布Gzip压缩数据?
Is there any way of posting Gzipped data using RestSharp?
推荐答案
请确保您已更新到RestSharp的最新版本(例如104.4.0),因为这是先前版本中的错误. 我认为这在104.2中已修复,其中二进制数据的PUT或POST以System.Byte []表示为字符串而结束.
Make sure you've updated to the latest version of RestSharp (like 104.4.0) as this was a bug in a previous version. I think this was fixed in 104.2 where the PUT or POST of binary data ended up with the System.Byte[] being represented as the string.
更新您的NuGet参考,然后重试.祝你好运!
Update your NuGet reference and try it again. Good luck!
这篇关于使用RestSharp发布GZip内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!