通过WebClient.DownloadData自动DECOM preSS GZIP响应 [英] Automatically decompress gzip response via WebClient.DownloadData
问题描述
我希望自动uncom preSS GZiped响应。 我使用下面的代码片段:
I wish to automatically uncompress GZiped response. I am using the following snippet:
mywebclient.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
mywebclient.Encoding = Encoding.UTF8;
try
{
var resp = mywebclient.DownloadData(someUrl);
}
我已经检查的Htt prequestHeader枚举
,并没有选择通过做到这一点的标题
I have checked HttpRequestHeader enum
, and there is no option to do this via the Headers
我怎么能自动DECOM preSS的注册教育储蓄计划?或者是否有另外一个功能,我应该使用,而不是 mywebclient.DownloadData code>?
How can I automatically decompress the resp? or Is there another function I should use instead of mywebclient.DownloadData
?
推荐答案
Web客户端使用HttpWebRequest的被窝里。而HttpWebRequest的支持gzip压缩/紧缩DECOM pression。请参阅<一href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.automaticdecom$p$pssion.aspx">HttpWebRequest AutomaticDecom pression财产
WebClient uses HttpWebRequest under the covers. And HttpWebRequest supports gzip/deflate decompression. See HttpWebRequest AutomaticDecompression property
然而,WebClient类没有这个属性直接公开。所以,你必须源于其上设置的属性的基本HttpWebRequest的。
However, WebClient class does not expose this property directly. So you will have to derive from it to set the property on the underlying HttpWebRequest.
class MyWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = base.GetWebRequest(address) as HttpWebRequest;
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
return request;
}
}
这篇关于通过WebClient.DownloadData自动DECOM preSS GZIP响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!