HttpClient does not send Accept-Encoding on Windows 2008 R2

I have a .NET Core 2.0 console application that executes a GET request.

It seems that the published version does not send headers Accept-Encodingfor compression on the test computer, but it works on my local machine.

I cannot find any other preliminary requests that could make the compression fail. Both of them support the .NET Core 2.1.4 SDK.

I tested the console application by running dotnet Console.dllin both environments.

  • Post to VS2017
  • Go to the output folder and run dotnet Console.dll. Confirm the title present in Fiddler.
  • Copy the entire output folder and deploy to the server
  • Run dotnet Console.dllagain and check for a missing header on the server using Fiddler.

I tried both HttpClient, and RestSharpand I am very puzzled.

A proof of concept that goes to a page that resonates with the request headers:

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

 using (var client = new HttpClient(handler))
 {
      response = client.GetStringAsync("http://scooterlabs.com/echo").Result;
 }

Local Environment (Win10)

GET http://scooterlabs.com/echo HTTP/1.1
Connection: Keep-Alive
Accept-Encoding: gzip, deflate
Host: scooterlabs.com

Server (Win2008 R2 on AWS)

GET http://scooterlabs.com/echo HTTP/1.1
Connection: Keep-Alive
Host: scooterlabs.com
+4
source share
3 answers

, WinHttp, HttpClient gzip\deflate Windows 8.1+. - WinHttp Accept-Encoding. , Windows Server 2008, .NET WinHttp - , , , , - .

(, client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));) - - , , . - WinHttp Content-Encoding, .NET. - , .NET , AutomaticDecompression.

, - Windows 8.1 , AutomaticDecompression, Accept-Encoding, , .

+2

, Azure Win Server 2008 R2 - (.NET Core SDK 2.1.4)

// http://scooterlabs.com/echo output

[headers] => Array
(
    [Connection] => Keep-Alive
    [Host] => scooterlabs.com
)


// http://httpbin.org/headers output
{
  "headers": {
    "Connection": "close",
    "Host": "httpbin.org"
  }
}

// https://postman-echo.com/headers output
{
    "headers": {
        "host": "postman-echo.com",
        "x-forwarded-port": "443",
        "x-forwarded-proto": "https"
    }
}

Win10 .

// http://scooterlabs.com/echo output
[headers] => Array
(
    [Connection] => Keep-Alive
    [Accept-Encoding] => gzip, deflate
    [Host] => scooterlabs.com
)

// http://httpbin.org/headers output
{
  "headers": {
    "Accept-Encoding": "gzip, deflate",
    "Connection": "close",
    "Host": "httpbin.org"
  }
}

// https://postman-echo.com/headers output
{
    "headers": {
        "host": "postman-echo.com",
        "accept-encoding": "gzip, deflate",
        "x-forwarded-port": "443",
        "x-forwarded-proto": "https"
    }
}

, HttpClient Accept-Encoding WS 2008 R2.

+2

Based on the answers of Ivan and Evk, it seems that this is a problem typical of older versions of Windows (older than Win8.1). Here's how to work and successfully handle compression in older versions of Windows.

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

   using (var client = new HttpClient(handler))
   {
        client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
        client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));
        response = client.GetStringAsync("http://scooterlabs.com/echo").Result;
   }

AutomaticDecompression must be installed in addition to the headers, otherwise you will get a compressed payload.

+2
source

Source: https://habr.com/ru/post/1693407/


All Articles