13

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
  • 143,130
  • 81
  • 406
  • 459
  • This can happen when the remote server returns really odd things like malformed headers. What are you posting to? Can you check the actual content to see if it's correct? – DavidG Aug 23 '17 at 15:25
  • I could probably figure out a way to do that if I could reproduce the error at will, @DavidG - but currently it seems to happen to me randomly and rarely, which makes doing any careful inspection to see exactly what's going along the wire a bit tricky. Hence asking here - to try and figure out what the *possibilities* even are for what could trigger this. – Mark Amery Aug 23 '17 at 15:26
  • Oh, transient and infrequent errors, the most fun of all the errors to debug! It's likely to be malformed headers I would guess, like `Header : Value` instead of `Header: Value`. Not much else I think we can help with here without seeing some actual returned data :/ – DavidG Aug 23 '17 at 15:31
  • 1
    You could [log all your responses](https://stackoverflow.com/questions/18924996/logging-request-response-messages-when-using-httpclient) until you get the error? – stuartd Aug 23 '17 at 15:44
  • 1
    This _may_ provide some insight. [https://github.com/dotnet/corefx/issues/14897](https://github.com/dotnet/corefx/issues/14897). – TnTinMn Aug 23 '17 at 17:08
  • It happened to me when I used "Bearer" instead of "Basic" in Authorization header. I was expecting a HTTP 401 to happen in this case, but... – heringer Apr 03 '19 at 20:45

2 Answers2

7

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
rism
  • 11,932
  • 16
  • 76
  • 116
-2

Probable case would be you did a http request without authorization token or header. If you are trying to call a secured method or api, it needs a secured token to be shared or validated in order to get or post data.

Kurkula
  • 6,386
  • 27
  • 127
  • 202
  • 2
    That seems unlikely. APIs typically return a 401 or 403 response in this case, which is still a valid HTTP response. – Mark Amery Mar 08 '19 at 12:41
  • @MarkAmery that is exactly what happened to me that caused this error. Calling http instead of https, you DONT get a valid http response. – crthompson Jul 24 '19 at 14:19
  • @paqogomez But this answer has nothing to do with HTTP vs HTTPS. – Mark Amery Jul 24 '19 at 15:03
  • 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 – crthompson Jul 24 '19 at 19:04
  • @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: https://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. – Mark Amery Jul 24 '19 at 19:29