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

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)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 21 (11 by maintainers)

Most upvoted comments

When that happens if the request is not processed before WebHostOptions.ShutdownTimeout that exception is thrown, so in our case increasing the value of that property fixed it.

Glad to hear that you were able to resolve the problem!