使用HttpClient和Jil消费压缩JSON
本文关键字:压缩 JSON Jil HttpClient 使用 | 更新日期: 2023-09-27 18:19:09
我对如何从HttpClient实例正确反序列化压缩Json有效负载有点困惑。
到目前为止,我正在做以下的事情,但它似乎是错误的。至少太复杂了。我不能给吉尔一条小溪吗?HttpClient不能解压缩流吗?var client = new HttpClient();
var userEndPoint = new Uri(baseUri, "api/login");
var request = new HttpRequestMessage();
request.RequestUri = userEndPoint;
request.Method = HttpMethod.Get;
var response = _client.SendAsync(request).Result;
var userGzipByteArray = response.Content.ReadAsByteArrayAsync().Result;
var outStream = new MemoryStream();
using (var gzStream = new GZipStream(userGzipByteArray , CompressionMode.Decompress))
{
gzStream.CopyTo(outStream);
}
var userByteArray = outStream.ToArray();
var userJson = userByteArray .ConvertToString();
var user = JSON.Deserialize<User>(userJson , Jil.Options.ISO8601PrettyPrintIncludeInherited);
您可以使用AutomaticDecompression
标志。参见.NET's HttpWebResponse自动解压缩gzipped和Deflated响应吗?