“服务器返回无效或无法识别的响应"的含义在 HttpClient 中 [英] Meaning of "The server returned an invalid or unrecognized response" in HttpClient

查看:116
本文介绍了“服务器返回无效或无法识别的响应"的含义在 HttpClient 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

等待 HttpClient.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 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆