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

Ger*_*ald 8 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);
Run Code Online (Sandbox Code Playgroud)

这将返回错误消息:

该请求已中止:无法创建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;
Run Code Online (Sandbox Code Playgroud)

正如从这里建议的那样,它有效。所以我猜RestSharp由于某种原因使用了错误的协议?

小智 19

即使对于 Restsharp 库,也可以使用以下行:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
Run Code Online (Sandbox Code Playgroud)

IRestResponse response = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)

将工作。


Don*_*dle 6

.NET 4.5TLS 1.2可用的,但不是默认启用

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

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


其他不错的SO答:

.NET 4.5中的默认SecurityProtocol


use*_*690 5

移动这一行: ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

在此行之前: WebRequest request = WebRequest.Create(url);

希望有帮助。