繁体   English   中英

C# Web 请求 w RestSharp - “请求被中止:无法创建 SSL/TLS 安全通道”

[英]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 安全通道

三件事:

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

他们的证书使用 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答案:

.NET 4.5 中的默认安全协议

即使对于使用以下行的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM