通过WebClient.DownloadData自动DECOM preSS GZIP响应DownloadData、WebClient、DECOM、GZIP

2023-09-02 01:22:47 作者:还有多少泪要流

我希望自动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 ?

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。请参阅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;
    }
}
 
精彩推荐
图片推荐