从Web客户端Uncom pressing GZIP响应 [英] Uncompressing gzip response from WebClient

查看:198
本文介绍了从Web客户端Uncom pressing GZIP响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种快速的方法来下载与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自动GZi​​ped和放气的反应?

这篇关于从Web客户端Uncom pressing GZIP响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆