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

15,521

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
Share:
15,521

Related videos on Youtube

Mark Amery
Author by

Mark Amery

Email address: [email protected]. No spam, please. I work for Curative Inc: https://curative.com/ I license you to use any of my Stack Overflow contributions in any way you like. If you find something wrong in one of my posts, feel free to edit it. I'm a frequent visitor and can always roll back if I think a change you've made is wrong or stupid, so you may as well be bold - it's better, here, to ask for forgiveness than permission.

Updated on June 07, 2022

Comments

  • Mark Amery
    Mark Amery about 2 years

    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?

  • Mark Amery
    Mark Amery over 5 years
    That seems unlikely. APIs typically return a 401 or 403 response in this case, which is still a valid HTTP response.
  • crthompson
    crthompson almost 5 years
    This was exactly my error. I had inadvertently switched to https, but had http in the config.
  • crthompson
    crthompson almost 5 years
    @MarkAmery that is exactly what happened to me that caused this error. Calling http instead of https, you DONT get a valid http response.
  • Mark Amery
    Mark Amery almost 5 years
    @paqogomez But this answer has nothing to do with HTTP vs HTTPS.
  • crthompson
    crthompson almost 5 years
    I think it does. OP states that doing an http request without token (which would make it https if it was with token). Trying to call a "secured" method is another indicator. While yes, I can see some ambiguity there, I think they are talking about http vs https
  • Mark Amery
    Mark Amery almost 5 years
    @paqogomez The TLS protocol used to encrypt HTTPS connections doesn't involve anything commonly referred to as a "token" as far as I know. The normal meaning of "authorization token" is this: en.wikipedia.org/wiki/Access_token - a token that identifies a login session or a user, usually sent in a cookie or a custom HTTP header. Unrelated to HTTPS.
  • user3502626
    user3502626 almost 5 years
    I am getting the same error. I changed Https to Http but I'm still getting the same error.
  • rism
    rism almost 5 years
    @user3502626 And? The question was not about how to fix the error - it asked how to reproduce it.
  • Nikunj Kakadiya
    Nikunj Kakadiya about 4 years
    It is working for me... Thank you thank you so much