.NET: Is it possible to get HttpWebRequest to automatically decompress gzip’d responses?

Use the HttpWebRequest.AutomaticDecompression property as follows:

HttpWebRequest hwr = (HttpWebRequest) WebRequest.Create(url);
hwr.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

It’s not necessary to manually add the Accept-Encoding HTTP header; it will automatically be added when that property is used.

(Also, I know this is just example code, but the HttpWebResponse object should be placed in a using block so it’s disposed correctly when you’ve finished using it.)

Leave a Comment