WP8 不支持 Gzip 压缩,但是有 3rd 方库可以让你这样做,我尝试了很多,但我无法让它工作。这是我最近的尝试:
var handler = new HttpClientHandler();
if (handler.SupportsAutomaticDecompression)
{
handler.AutomaticDecompression = DecompressionMethods.GZip |
DecompressionMethods.Deflate;
}
Uri myUri = new Uri("http://www.web.com");
HttpClient client = new HttpClient(handler);
client.BaseAddress = myUri;
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("ubq-compression", "gzip");
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, myUri);
req.Content = new StringContent(finalURL, Encoding.UTF8);
HttpResponseMessage rm = client.SendAsync(req).Result;
string rst = await rm.Content.ReadAsStringAsync();
API返回给我一个字节数组,但前300个不是Gziped,但其他一切都是
我需要解压缩 300 字节之后的所有内容。
我正在使用http://www.nuget.org/packages/Microsoft.Net.Http
// 我正在拆分数组 byte[] hJ = res.Take(300).ToArray(); byte[] bJ = res.Skip(300).ToArray();
bj 是需要解压的。
我正在尝试这个
MemoryStream 流 = 新的 MemoryStream(); stream.Write(bj, 0, bj.Length);
using (var inStream = new MemoryStream(bj))
{
var bigStreamsss = new GZipStream(inStream, CompressionMode.Decompress, true);
using (var bigStreamOut = new MemoryStream())
{
bigStreamsss.CopyTo(bigStreamOut);
output = Encoding.UTF8.GetString(bigStreamOut.ToArray(), 0, bigStreamOut.ToArray().Length);
}
}
但它总是在这条线上失败“ var bigStreamsss = new GZipStream(inStream, CompressionMode.Decompress, true); ”
任何帮助将非常感激