Httpwebrequest & Native Gzip Compression

C# HttpWebRequest - Using Gzip Compression

Make sure you state in your request that you can accept gzip'd responses. So, after your code to create the request, do this:

request.Headers.Add("Accept-Encoding", "gzip");

This will tell the server to return it zipped, if it can.

The only way I can think to show the size difference would be t make 2 calls, one with compression and one not, and compare the length of the returned response streams.

Does .NET's HttpWebResponse uncompress automatically GZiped and Deflated responses?

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.

How can I decompress GZIP/DEFLATE content with HTTPWebRequest

When you're adding the Accept-Encoding header, you're telling the web server: "Please send me a response that is compressed using GZIP or DEFLATE."

The webserver dutifully returns such a response and your client gets the compressed content. If you want to decompress that content, you must do so manually if your object does not support automatic decompression.

Rather than adding the Accept-Encoding header manually, instead tell .NET to do it and to automatically decompress the response on your behalf.

htmlRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

Automatically decompress gzip response via WebClient.DownloadData

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;
}
}

Decompressing GZip Stream from HTTPClient Response

Just instantiate HttpClient like this:

HttpClientHandler handler = new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};

using (var client = new HttpClient(handler)) //see update below
{
// your code
}

Update June 19, 2020:
It's not recommended to use httpclient in a 'using' block as it might cause port exhaustion.

private static HttpClient client = null;

ContructorMethod()
{
if(client == null)
{
HttpClientHandler handler = new HttpClientHandler()
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
};
client = new HttpClient(handler);
}
// your code
}

If using .Net Core 2.1+, consider using IHttpClientFactory and injecting like this in your startup code.

 var timeout = Policy.TimeoutAsync<HttpResponseMessage>(
TimeSpan.FromSeconds(60));

services.AddHttpClient<XApiClient>().ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler
{
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
}).AddPolicyHandler(request => timeout);

OutputCache and a custom gzip compression filter

Have a look at this page, http://www.klopfenstein.net/lorenz.aspx/my-take-on-asp-net-output-caching
There are some good info there especially Jeff Atwood's advice on compressing cache items

From the page..

Order is important

The ActionFilter above must absolutely be run as last: as I discovered lately, as soon as an action filter changes the action result, the current action invocation is aborted. This also means that all other action filters which did not have a chance to run, will not run, ever. If you plan on adding this caching method to your project, make sure that all filters have the right priority (using the Order priority, that takes a positive integer and orders from lowest to highest).



Related Topics



Leave a reply



Submit