runtime: HttpClient no longer sends header 'Accept-Encoding:"gzip, deflate"' and decompress 'gzip/deflate' content by default in .NET Core 2.0

This is a breaking change compared to netcoreapp1.1. In netcoreapp1.1, HttpClient automatically sends header "Accept-Encoding": "gzip, deflate" in the request, and automatically decompress ‘gzip/deflate’ content from the response. However, with netcoreapp2.0, HttpClient doesn’t send the header and stop automatically decompressing ‘gzip/deflate’ content.

Repro Steps

// Program.cs
using System;
using System.Net.Http;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Get, @"http://httpbin.org/headers"))
                {
                    var result = client.SendAsync(request).Result;
                    var payload = result.Content.ReadAsStringAsync().Result;
                    Console.WriteLine("Header Dict:\n{0}", payload);
                }
                
                using (var request = new HttpRequestMessage(HttpMethod.Get, @"http://httpbin.org/gzip"))
                {
                    var result = client.SendAsync(request).Result;
                    var payload = result.Content.ReadAsStringAsync().Result;
                    Console.WriteLine("Decompressed Content:\n{0}", payload);
                }
            }
        }
    }
}

With netcoreapp1.1 http://httpbin.org/headers just echos the request headers. You can see Accept-Encoding was sent by HttpClient. http://httpbin.org/gzip returns gzip-encoded data. You can see the content was automatically decompressed by HttpClient.

PS> dotnet restore
PS> dotnet run
Header Dict:
{
  "headers": {
    "Accept-Encoding": "gzip, deflate",
    "Connection": "close",
    "Host": "httpbin.org"
  }
}

Decompressed Content:
{
  "gzipped": true,
  "headers": {
    "Accept-Encoding": "gzip, deflate",
    "Connection": "close",
    "Host": "httpbin.org"
  },
  "method": "GET",
  "origin": "131.107.147.177"
}

With netcoreapp2.0 Accept-Encoding was not sent by HttpClient and the gzip-encoded data was not automatically decompressed.

PS> dotnet restore
PS> dotnet run
Header Dict:
{
  "headers": {
    "Connection": "close",
    "Host": "httpbin.org"
  }
}

Decompressed Content:
 W??X?%?A
?0E?9E???
nE? ^??C2????K??]??????r?.V?O?????*[5*?rJ8qR?+?>???U?"?E?q      ??v?>(?????.h7??;????????

Environment

netcoreapp1.1

.NET Command Line Tools (1.0.1)

Product Information:
 Version:            1.0.1
 Commit SHA-1 hash:  005db40cd1

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.14393
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   ...\AppData\Local\Microsoft\dotnet\sdk\1.0.1

netcoreapp2.0

.NET Command Line Tools (2.0.0-preview1-005724)

Product Information:
 Version:            2.0.0-preview1-005724
 Commit SHA-1 hash:  e391b5d5f3

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.14393
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   ...\AppData\Local\Microsoft\dotnet\sdk\2.0.0-preview1-005724\

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (14 by maintainers)

Most upvoted comments

Summary:

On .NET Core 1.1, the default value for the HttpClientHandler.AutomaticDecompression property was

DecompressionMethods.Deflate | DecompressionMethods.GZip

On .NET Core 2.0, the default is now:

DecompressionMethods.None;

This was changed so that it is now the same as .NET Framework.

If you need to change the default back to what it was in .NET Core 1.1, use the following code:

var handler = new HttpClientHandler();
handler.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
var client = new HttpClient(handler);