C#: Enable Automatic Decompression on System.Net.WebClient

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;
	}
}

3 thoughts on “C#: Enable Automatic Decompression on System.Net.WebClient”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: