50

I am trying to do a request that accepts a compressed response

var request = (HttpWebRequest)HttpWebRequest.Create(requestUri);
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");

I wonder if when I add the second line I will have to handle the decompression manually.

Jader Dias
  • 88,211
  • 155
  • 421
  • 625
  • I guess Rick Strahl must be wrong. Have you tried it yourself? – Keltex Mar 24 '09 at 18:29
  • thanks - just trying to understand things, can you tell me if this is correct? (a) if you do NOT add this "AcceptEncoding" line then - if you download a non-compressed file => works FINE - if you download a compressed file => ISSUE (will download file but will look corrupt, as wasn't uncompressed) (b) if you DO add this "AcceptEncoding" line then: - if you download a non-compressed file => still works FINE - if you download a compressed file => works fine (will be uncompressed) Is this right? – Greg Sep 28 '09 at 00:05
  • 1
    @Greg None of the options. The second line isn't about downloading files that maybe already compressed (ie. zip files), but about downloading web resources that may be compressed on-demand (ie. html files). If you do not put the second line, a well behaved web server will not send you compressed files at all. If the web server has the ability to compress files it will do only if you put the second line. – Jader Dias Sep 28 '09 at 00:51
  • upvote for the question too :) – sumit_batcoder May 10 '11 at 07:55

4 Answers4

117

I found the answer.

You can change the code to:

var request = (HttpWebRequest)HttpWebRequest.Create(requestUri);
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

And you will have automatic decompression. No need to change the rest of the code.

skolima
  • 31,963
  • 27
  • 115
  • 151
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
  • 5
    Technically you just need the one line, "AutomaticDecompression". It seems to automatically add "gzip,deflate" to the headers. – LongZheng Feb 02 '12 at 14:35
  • Are there any issues with leaving automatic decompression set even if the response isn't compressed? According to this it's normal for the server not to compress every response: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding – Joe Eng Nov 02 '18 at 19:48
  • If you need Brotli support you may consider doing it yourself like [in this answer](https://stackoverflow.com/a/60522090/659778). – Louis Somers Jul 27 '22 at 09:37
4

For .NET Core things are a little more involved. A GZipStream is needed as there isn't a property (as of writing) for AutomaticCompression Consider the following GET example:

var req = WebRequest.CreateHttp(uri);

/*
 * Headers
 */
req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";

/*
 * Execute
 */
try
{
    using (var resp = await req.GetResponseAsync())
    {
        using (var str = resp.GetResponseStream())
        using (var gsr = new GZipStream(str, CompressionMode.Decompress))
        using (var sr = new StreamReader(gsr))

        {
            string s = await sr.ReadToEndAsync();  
        }
    }
}
catch (WebException ex)
{
    using (HttpWebResponse response = (HttpWebResponse)ex.Response)
    {
        using (StreamReader sr = new StreamReader(response.GetResponseStream()))
        {
            string respStr = sr.ReadToEnd();
            int statusCode = (int)response.StatusCode;

            string errorMsh = $"Request ({url}) failed ({statusCode}) on, with error: {respStr}";
        }
    }
}
pim
  • 12,019
  • 6
  • 66
  • 69
-1

GZIP and Deflate responses are not automatically handled. See this article for the details: HttpWebRequest and GZip Http Responses

Jeroen Landheer
  • 9,160
  • 2
  • 36
  • 43
  • Sorry, I was mixing this one up with the HttpListener's request & response classes. The standard web request in .Net does indeed handle compressed responses. – Jeroen Landheer Oct 18 '09 at 20:33
-2

I think you have to decompress the stream yourself. Here's an article on how to do it:

http://www.west-wind.com/WebLog/posts/102969.aspx

Keltex
  • 26,220
  • 11
  • 79
  • 111
  • Good find. Looks like this was added in .NET 2.0. Maybe Strahl when wrote his article he was used to 1.1 – Keltex Mar 24 '09 at 18:40
  • Using HttpWebRequest.AutomaticDecompression automatically adds the proper request headers and handles the decompression. – Armbrat Oct 06 '09 at 19:25
  • I dont think AutomaticDecompression will add "Accept-Encoding"=gzip, deflate to headers. I am using .NETCF3.5 and Headers do not have this added unless I add them using request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate"); – pixel Sep 07 '15 at 04:04