views:

1310

answers:

4

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.

A: 

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
see my answer below
Jader Dias
Good find. Looks like this was added in .NET 2.0. Maybe Strahl when wrote his article he was used to 1.1
Keltex
Using HttpWebRequest.AutomaticDecompression automatically adds the proper request headers and handles the decompression.
Joe
A: 

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

Jeroen Landheer
This is not true.
Joe
Jeroen Landheer
+9  A: 

I found the answer.

You can add the third line below:

var request = (HttpWebRequest)HttpWebRequest.Create(requestUri);
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

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

Jader Dias
A: 

Jader Dias, you are a god send! request.AutomaticDecompression = DecompressionMethods.GZip; fixed a problem I have been racking my brain over for the past 12 hours straight!

Levi