Skip to content

System.IO.IOException: The server returned an invalid or unrecognized response #26774

@mayconbeserra

Description

@mayconbeserra

Hi,

I'm having an issue when calling one API which requires certificates. To reproduce we have to execute inside a docker container.

This error doesn't happen on macOS anymore since I migrated to .NET Core 2.1, but it still persists on Docker containers.

Framework: .NET Core 2.1

Docker file uses the following base image: FROM microsoft/dotnet:2.1-runtime

Code as example:

          var cert = new X509Certificate2("./certificate.pfx", "password");
                       
            Console.WriteLine("Has private key: " + cert.HasPrivateKey);
            Console.WriteLine("Chain is valid: " + cert.Verify());
            Console.WriteLine("Version: " + cert.Version);
            handler.ClientCertificates.Add(cert);

            HttpClient client = new HttpClient(handler);

            try
            {
              
                var userpwd = Encoding.ASCII.GetBytes("user:password");
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(userpwd));
                client.DefaultRequestHeaders.Add("X-ApplicationName", "APP");
                client.DefaultRequestHeaders.Add("X-TrackingId", "TEST123");
                client.DefaultRequestHeaders.Add("accept", "application/json");
                client.Timeout = TimeSpan.FromSeconds(60);

                client.MaxResponseContentBufferSize = 2147483647;

                var response = await client.GetAsync
                (
                    requestUri: "https://www.myurl.com"
                );

                Console.WriteLine("Result: " + await response.Content.ReadAsStringAsync());
                Console.WriteLine(response.Headers.ToString());
                Console.WriteLine(response.Content.Headers.ToString());
                Console.WriteLine(response.ReasonPhrase);
                
            } catch (Exception ex)
            {
                System.Console.WriteLine("Exception: {0}", ex.ToString());
            }

Error:

Exception: System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.IO.IOException: The server returned an invalid or unrecognized response.
   at System.Net.Http.HttpConnection.FillAsync()
   at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at app_cert.Program.GetDataAsync2() in /##############/Program.cs:line 112
Inner Exception: System.IO.IOException: The server returned an invalid or unrecognized response.
   at System.Net.Http.HttpConnection.FillAsync()
   at System.Net.Http.HttpConnection.ReadNextResponseHeaderLineAsync(Boolean foldedHeadersAllowed)
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions