You should log the status code, status text, and the body if any.
.NET 6 : System.Net.Http.HttpRequestException: Received an invalid status line: '339'
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 :
- 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)
- System.Net.Http.HttpRequestException: Received an invalid status line: '33b'. ....
- System.Net.Http.HttpRequestException: Received an invalid status line: '341'. ....
- System.Net.Http.HttpRequestException: Received an invalid status line: '345'. ....
- System.Net.Http.HttpRequestException: Received an invalid status line: '33b'. ....
- 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!
3 answers
Sort by: Most helpful
-
-
Michael Taylor 54,401 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.
-
SurferOnWww 2,906 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?
If so, consider using the pool HTTP connections with HttpClientFactory as described below: