I have a program which makes lots of HttpWebRequests, and I read about using gzip compression to speed up the downloading of the response data. I know what gzip is, and how it works, but I don't know how it works in C#.
Let's say I have a simple GET request:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://google.com");
request.Method = "GET";
WebResponse response = request.GetResponse();
How could I make the response data be compressed in gzip? How can I show the compressed size, and then the un-compressed size?
Thanks
See this page for a synopsis of how GZip works with web requests in general.
The specific header you're going to need to send is Accept-Encoding: gzip. Normally, you'd need to add this to the request yourself (via the Headers collection), but there's a shortcut. Per this answer, all you need to do is add
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
to your code.
Note that you probably won't be able to get the compressed/uncompressed sizes doing this - the request will automatically decompress it as it comes in. However, since you can't know ahead of time whether the server will even try to send you GZipped pages, it's not a useful thing to be testing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With