Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of "The server returned an invalid or unrecognized response" in HttpClient

When awaiting an HttpClient.PostAsync response, I sometimes see an error stating "The server returned an invalid or unrecognized response":

System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.Http.WinHttpException: The server returned an invalid or unrecognized response
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.WinHttpHandler.<StartRequest>d__105.MoveNext()
   --- End of inner exception stack trace ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()
   at System.Net.Http.HttpClient.<FinishSendAsync>d__58.MoveNext()

That's a pretty cryptic error; I suppose I can conceive of what an "invalid" response might possibly be (i.e. a syntactically-invalid HTTP response, like one that jumps straight into headers without a status line), but what on earth is an "unrecognized" response to a HTTP request? The message seems like nonsense.

What circumstances can actually trigger this error, under the hood?

like image 376
Mark Amery Avatar asked Aug 23 '17 15:08

Mark Amery


1 Answers

You can reproduce this 100% of the time when calling an https only endpoint using an http schemed call. So for instance calling an API at:

https://www.mysecureapi.com

using

http://www.mysecureapi.com
like image 88
rism Avatar answered Oct 23 '22 16:10

rism