【问题标题】:HTTPWebRequest with gzip compression使用 gzip 压缩的 HTTPWebRequest
【发布时间】:2014-11-11 22:59:14
【问题描述】:

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

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

如何在 HTTP Header 中设置 Accept-Encoding?

【问题讨论】:

  • How can I set the Accept-Encoding in the HTTP Header? request.Headers[HttpRequestHeader.AcceptEncoding] = "bla bla"; 你试过这个吗?
  • 谢谢。这样可行。你能给我一个例子,我怎样才能解压json数据来读取它,好吗?

标签: c# json httpwebrequest gzip


【解决方案1】:

将请求的AutomaticDecompression属性设置为GZip

request.AutomaticDecompression = DecompressionMethods.GZip;

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


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

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

【讨论】:

  • 自动解压不可用。我正在编写 WinRT 应用程序
猜你喜欢
  • 1970-01-01
  • 2013-10-01
  • 2010-10-24
  • 1970-01-01
  • 2015-07-14
  • 2012-06-17
  • 2012-09-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多