c# - HttpClientHandler 抛出 PlatformNotSupportedException

标签 c# ssl asp.net-core httpclient

以下代码抛出“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/

相关文章:

c# - 如何从针对完整 .NET Framework 的 .NET SDK csproj 中删除默认/锁定的程序集引用?

java - 使用 EclipseLink JPA 的 SSL

ssl - Netty SSL 模式奇怪的行为

c# - OData 和 Cosmos DB

c# - 如何在 LINQ 上使用 OR 连接 where 子句?

c# - 创建用于上传图像的 ViewModel 的正确方法是什么?

c# - 在 .NET Core Web API 中为 MongoDB 使用 OData

mysql - 在 asp.net core 中不活动后注销用户

c# - 无法使用接口(interface)确定 i => i.Id 的序列化信息

php - 亚马逊 SES 错误