从Web客户端Uncom pressing GZIP响应 [英] Uncompressing gzip response from WebClient
问题描述
有没有一种快速的方法来下载与WebClient.DownloadString()方法uncom preSS gzip的回应?你对如何处理gzip的响应与Web客户端有什么建议?
Is there a quick way to uncompress gzip response downloaded with WebClient.DownloadString() method? Do you have any suggestions on how to handle gzip responses with WebClient?
推荐答案
要做到这一点,最简单的方法是使用内置的<一个href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.automaticdecom$p$pssion.aspx">automatic DECOM pression 与的HttpWebRequest
类。
The easiest way to do this is to use the built in automatic decompression with the HttpWebRequest
class.
var request = (HttpWebRequest)HttpWebRequest.Create("http://stackoverflow.com");
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
要做到这一点的, Web客户端
,你必须让自己的类从 Web客户端导出
并覆盖 GetWebRequest()
方法。
To do this with a WebClient
you have to make your own class derived from WebClient
and override the GetWebRequest()
method.
public class GZipWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
return request;
}
}
另请参阅该SO主题:<一href="http://stackoverflow.com/questions/678547/does-nets-httpwebresponse-uncom$p$pss-automatically-gziped-and-deflated-response">Does .NET的HttpWebResponse uncom preSS自动GZiped和放气的反应?
这篇关于从Web客户端Uncom pressing GZIP响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!