.NET 6 : System.Net.Http.HttpRequestException: Received an invalid status line: '339'

Mohd Imran Nagori 0 Reputation points
2024-07-16T14:21:17.7633333+00:00

Hello,

We are experiencing an issue where our service, calls an API. It fails when invokes the API using the HTTP client. Specifically, the process fails at the EnsureSuccessStatusCode() step, despite API returning a success response. This issue appears in about 1% of the HTTP requests, which is puzzling.

Below, I've included some relevant error logs from the System.Net.Http library for reference.

Error Trace :

  1. System.Net.Http.HttpRequestException: Received an invalid status line: '339'.\n at System.Net.Http.HttpConnection.ParseStatusLine(ReadOnlySpan`1 line, HttpResponseMessage response)\n at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)\n at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)\n at System.Net.Http.HttpConnectionPool.SendWithVersionDetectionAndRetryAsync(HttpRequestMessage request, Boolean async, Boolean doRequestAuth, CancellationToken cancellationToken)\n at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, Boolean async, CancellationToken cancellationToken)\n at Microsoft.Extensions.Http.Logging.LoggingHttpMessageHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\n at Cards.InstantIssue.Infrastructure.Handlers.CorrelationIdHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
  2. System.Net.Http.HttpRequestException: Received an invalid status line: '33b'. ....
  3. System.Net.Http.HttpRequestException: Received an invalid status line: '341'. ....
  4. System.Net.Http.HttpRequestException: Received an invalid status line: '345'. ....
  5. System.Net.Http.HttpRequestException: Received an invalid status line: '33b'. ....
  6. System.Net.Http.HttpRequestException: Received an invalid status line: '33f'. ....

.....

Has anyone else experienced similar behavior? If so, any insights or potential workarounds would be greatly appreciated.

Technical details :

.NET 6

Any suggestions or help would be highly appreciated. Thank you in advance for your assistance!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,385 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
318 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,266 Reputation points
    2024-07-16T15:08:45.58+00:00

    You should log the status code, status text, and the body if any.


  2. Michael Taylor 51,341 Reputation points
    2024-07-16T19:35:54.1766667+00:00

    My gut instinct is that maybe you aren't calling the endpoint in prod that you should be. Personally I would set a breakpoint on the EnsureSuccessStatusCode call and replicate the problem. Then use the debugger to look at the HTTP headers and optionally the content body. I wonder if maybe the response you're getting isn't actually an HTTP response, although I'm not sure how you'd be doing that.

    If setting a breakpoint isn't an option then use Fiddler or similar tool to look at the request and response data and see what it looks like.

    Another possibility is that the API you're calling is just wrong. If it is an established API then this isn't likely but if it is a new one then maybe they messed up.


  3. SurferOnWww 2,486 Reputation points
    2024-07-17T02:13:15.62+00:00

    It fails when invokes the API using the HTTP client.

    Does the "HTTP client" mean the HttpClient Class?

    HttpClient Class

    If so, consider using the pool HTTP connections with HttpClientFactory as described below:

    Pool HTTP connections with HttpClientFactory