![](/img/trans.png)
[英]C# Console application - The request was aborted: Could not create SSL/TLS secure channel using Restsharp
[英]C# Web Request w RestSharp - "The request was aborted: Could not create SSL/TLS secure channel"
我对 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);
这将返回错误消息:
请求被中止:无法创建 SSL/TLS 安全通道
三件事:
他们的证书使用 TLS 1.2 和 AES 128,因此它与 RC4 引起的错误无关。
这是在 Visual Studio 2015 中我的本地 Win 10 机器上,目标框架为 .NET 4.5.2。
为什么我会收到此错误?
编辑:
通过更改我的代码以使用基本 System.Net 和 WebRequest 类并添加以下行:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
正如从这里建议的那样,它有效。 所以我猜 RestSharp 出于某种原因使用了不正确的协议?
在.NET 4.5
中, TLS 1.2
可用,但默认情况下未启用。
所以是的,如果您需要TLS 1.2
,您需要以某种方式为 .NET 运行时指定它。
仅供参考:在.NET 4.7
中,.NET 将使用操作系统的默认 SSL/TLS 版本(大多数现代操作系统将TLS 1.2
作为默认版本)
其他好的SO答案:
即使对于使用以下行的 Restsharp 库:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
前
IRestResponse response = client.Execute(request);
将工作。
移动这一行:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
在此行之前:
WebRequest request = WebRequest.Create(url);
希望有帮助。
尝试这个...
ServicePointManager.ServerCertificateValidationCallback = new
RemoteCertificateValidationCallback
(
delegate { return true; }
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.