以下代码抛出“PlatformNotSupportedException”“此平台不支持操作”
它是一个 NET 标准库(尝试针对 1.4 和 2.0 进行编译),被作为 Web 应用程序运行的 .NET 4.6.1 项目引用。
var handler = new HttpClientHandler();
handler.SslProtocols = SslProtocols.Tls12;
为什么 Tls12 抛出这个异常,解决方法是什么?
最佳答案
这里的问题是 SslProtocols
属性在 .NET Framework 上不存在,它只存在于 .NET Core 中。
您可以在 HttpClientHandler
的文档中看到这一点。
在 .NET Framework 中,您必须通过 ServicePointManager.SecurityProtocol
进行设置,即
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11;
要在您的 .NET Standard PCL 中实现这一点,您很可能必须交叉编译到 netstandard-2.0 和 net461,因为我不确定它是否仍然存在于 .NET Standard 中/.NET 核心。
编辑:
或者,从您的 PCL 中删除它,并通过上面的静态属性在您的 .NET Framework 应用程序中全局设置它。
关于c# - HttpClientHandler 抛出 PlatformNotSupportedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46400797/