Thursday, August 05, 2010

GZip your downloads

Gonzalo yesterday pointed me to a feature in the HTTP client stack for .NET that I did not know about.
If you want the server to gzip the response before sending it to you, set the AutomaticDecompression flag in your HttpWebRequest:
var request = (HttpWebRequest) WebRequest.Create (uri);
request.AutomaticDecompression = DecompressionMethods.GZip;
This will set the Accept-Encoding HTTP header to gzip when you make your connection and automatically decompress this for you when you get the response stream.
Update: on the comments there is a suggestion that Deflate is another option you can use, and you can combine both GZip + Deflate on the flags above.
Read more: Miguel de Icaza's web log