Automatically decompress gzip response via WebClient.DownloadData

.NetGzipWebclient

.Net Problem Overview


I wish to automatically uncompress GZiped response. I am using the following snippet:

mywebclient.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
mywebclient.Encoding = Encoding.UTF8;

try
{
 	var resp = mywebclient.DownloadData(someUrl);
}

I have checked HttpRequestHeader enum, and there is no option to do this via the Headers

How can I automatically decompress the resp? or Is there another function I should use instead of mywebclient.DownloadData ?

.Net Solutions


Solution 1 - .Net

WebClient uses HttpWebRequest under the covers. And HttpWebRequest supports gzip/deflate decompression. See HttpWebRequest AutomaticDecompression property

However, WebClient class does not expose this property directly. So you will have to derive from it to set the property on the underlying HttpWebRequest.

class MyWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest request = base.GetWebRequest(address) as HttpWebRequest;
        request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
        return request;
    }
}

Solution 2 - .Net

Depending on your situation, it may be simpler to do the decompression yourself.

using System.IO.Compression;
using System.Net;

try
{
	var client = new WebClient();
    client.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
	var responseStream = new GZipStream(client.OpenRead(myUrl), CompressionMode.Decompress);
	var reader = new StreamReader(responseStream);
	var textResponse = reader.ReadToEnd();

	// do stuff

}

I created all the temporary variables for clarity. This can all be flattened to only client and textResponse.

Or, if simplicity is the goal, you could even do this using ServiceStack.Text by Demis Bellot:

using ServiceStack.Text;

var resp = "some url".GetJsonFromUrl();

(There are other .Get*FromUrl extension methods)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJulius AView Question on Stackoverflow
Solution 1 - .NetferozeView Answer on Stackoverflow
Solution 2 - .NetBen CollinsView Answer on Stackoverflow