The WebClient class is really easy to use, but actually doesn’t provide so much control over the underline request, or so it seems, but we can still inherits from it to get the control over the WebRequest that the WebClient using.
With this in mind I’ve could extend the WebClient enabling the powerfull AutomaticDecompression of the HttpWebRequest, and getting compressed web resources:
public class AutomaticDecompressionWebClient : WebClient { protected override WebRequest GetWebRequest(Uri address) { var request = base.GetWebRequest(address) as HttpWebRequest; if (request == null) throw new InvalidOperationException("You cannot use this WebClient implementation with an address that is not an http uri."); request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; return request; } }
Thank you very much! So simple, and it works!
Thank you Mark M for the feedback!