问题描述
在等待 .PostAsync
响应,有时我会看到一条错误消息,指出服务器返回了无效或无法识别的响应 :
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()
那是一个相当神秘的错误;我想我可以设想出一个无效响应可能是什么(即语法无效的HTTP响应,例如直接跳到没有状态行的标头中的响应),但实际上是对HTTP的无法识别响应请求?该消息似乎是胡说八道。
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?
推荐答案
使用http计划的调用仅调用https终结点时,您可以在100%的时间内重现此内容。因此,例如在以下位置调用API:
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
使用
http://www.mysecureapi.com
这篇关于意思是“服务器返回了无效或无法识别的响应”。在HttpClient中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!