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;
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;
}
}
Also see this SO thread: Does .NET's HttpWebResponse uncompress automatically GZiped and Deflated responses?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…