异常:无法从传输连接读取数据:现有连接被远程主机强行关闭

Exception : Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host

我正在尝试使用 HTTPClient 调用 API,但我收到标题中提到的异常。

经过 Whosebug 的一些研究后,我发现在服务调用之前添加这一行可以解决问题

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

但是此人没有解释这一行的原因,并且该行的大小写问题是否适用于程序的任何其他部分。

注意:我连接的服务器使用 https。

提前致谢。

MSDN 文章中您会发现必须在调用 HTTPS 端点之前设置此 属性。

This property selects the version of the Secure Sockets Layer (SSL) or Transport Layer Security (TLS) protocol to use for new connections that use the Secure Hypertext Transfer Protocol (HTTPS) scheme only; existing connections are not changed. Note that no default value is listed for this property, on purpose. The security landscape changes constantly, and default protocols and protection levels are changed over time in order to avoid known weaknesses. Defaults will vary depending on individual machine configuration, and on which software is installed, and on which patches have been applied.