Requesting a compressed Gzip response(C#)

From the server, you can request a compressed response(Gzip) to be send back. The following code explains what you should add and replace to enable this compression.

  1. First add this: using System.IO.Compression;

  2. Next add the following to the request headers:
    webRequest.Headers.Add(System.Net.HttpRequestHeader.AcceptEncoding, "gzip");

  3. After creating a response add this code to get the response stream:
    StreamReader str;
    //The following is to check if the server sending the response supports Gzip
    if (webResponse.Headers.Get("Content-Encoding") != null &&
    webResponse.Headers.Get("Content-Encoding").ToLower() == "gzip")
    str = new StreamReader(new GZipStream(webResponse.GetResponseStream(), CompressionMode.Decompress));
    str = new StreamReader(webResponse.GetResponseStream());