具有gzip压缩的HTTPWebRequest

本文关键字:HTTPWebRequest 压缩 gzip 具有 | 更新日期: 2023-09-27 18:30:01

如果我从Web服务获取json数据,我如何激活gzip压缩?这是我的代码:

HttpWebRequest request = HttpWebRequest.CreateHttp(url);
WebResponse response = await request.GetResponseAsync();

如何在HTTP标头中设置Accept Encoding?

具有gzip压缩的HTTPWebRequest

将请求的AutomaticDecompression属性设置为GZip:

request.AutomaticDecompression = DecompressionMethods.GZip;

(如果您想同时接受GZipDeflate,也可以将其设置为两者的组合)


EDIT:在WinRT中,此属性不可用。但是无论如何,在WinRT中,应该使用HttpClient类,而不是HttpWebRequestAutomaticDecompression属性在HttpClientHandler类上可用:

var handler = new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip };
var client = new HttpClient(handler);