C# Web 请求 w RestSharp - "The request was aborted: Could not create SSL/TLS secure channel"

标签 c# restsharp

我有一个使用 RestSharp 的非常简单的 Web 请求:

var client = new RestClient("https://website.net");
var request = new RestRequest("/process", Method.GET);
request.AddParameter("cmd", "execute");
IRestResponse response = client.Execute(request);

var content = response.Content;
Console.WriteLine("Response: " + content);

这将返回错误消息:

The request was aborted: Could not create SSL/TLS secure channel

三件事:

  1. 我通过浏览器得到了我期望的响应,
  2. 我通过 Postman 得到了我期望的响应,
  3. 此请求正在发送到测试环境,但我可以将其发送到具有非常相似地址的生产环境,并获得我期望的响应,
  4. 我确信它在今天之前有效。

他们的证书使用 TLS 1.2 和 AES 128,因此它与 RC4 引起的错误无关。

这是在我的 Visual Studio 2015 本地 Win 10 机器上,目标框架是 .NET 4.5.2。

为什么会出现此错误?

编辑:

通过更改我的代码以使用基本 System.Net 和 WebRequest 类并添加以下行:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

根据 here 的建议, 有用。所以我猜 RestSharp 出于某种原因使用了不正确的协议(protocol)?

最佳答案

.NET 4.5 中,TLS 1.2 可用不可用 em> 默认启用。

是的,如果您需要 TLS 1.2,您需要以某种方式为 .NET 运行时指定它。

仅供引用: 在 .NET 4.7 中,.NET 将使用操作系统的默认 SSL/TLS 版本(大多数现代操作系统将默认使用 TLS 1.2)


其他好的SO回答:

Default SecurityProtocol in .NET 4.5

关于C# Web 请求 w RestSharp - "The request was aborted: Could not create SSL/TLS secure channel",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49885842/

相关文章:

c# - 在 C# 中选择搜索结果的类型

c# - 是否可以通过 Azure 辅助角色中的 .NET 代码进行打印

c# - 如何使用 restsharp 上传多个文件?

c# - 使用 restsharp 序列化对象并将其传递给 WebApi 而不是序列化列表

c# - .net FlagsAttribute 枚举是否需要手动赋值?

c# - 当基类中只需要依赖时,在基类中使用属​​性注入(inject)是否合适?

c# - 使用 UI 按钮移动游戏对象

c# - RestSharp 超时不起作用

c# - 将代码从 RestSharp 转换为 HttpClient

c# - 在 C# 中反序列化嵌套 JSON 数组时出现问题