我有一个使用 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
三件事:
- 我通过浏览器得到了我期望的响应,
- 我通过 Postman 得到了我期望的响应,
- 此请求正在发送到测试环境,但我可以将其发送到具有非常相似地址的生产环境,并获得我期望的响应,
- 我确信它在今天之前有效。
他们的证书使用 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回答:
关于C# Web 请求 w RestSharp - "The request was aborted: Could not create SSL/TLS secure channel",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49885842/