【问题标题】:C# - RestSharp - The request was aborted: Could not create SSL/TLS secure channelC# - RestSharp - 请求被中止:无法创建 SSL/TLS 安全通道
【发布时间】:2019-08-10 13:44:55
【问题描述】:

我正在尝试使用 RestSharp 调用 REST Web 服务。

以下是我的代码:

        ServicePointManager.Expect100Continue = true;
        ServicePointManager.DefaultConnectionLimit = 9999;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3;
        ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;

        var client = new RestClient(url);
        var certificate = new X509Certificate();

        certificate.Import(Properties.Resources.cert, "password", X509KeyStorageFlags.PersistKeySet);
        client.ClientCertificates = new X509CertificateCollection() { certificate };

        var request = new RestRequest(Method.POST);
        request.AddHeader("Connection", "keep-alive");
        request.AddHeader("Content-Type", "text/plain");
        request.AddParameter("undefined", $"param={param}", ParameterType.RequestBody);
        IRestResponse response = await client.ExecutePostTaskAsync(request);

        return response;

但是,我收到错误 - 请求被中止:无法创建 SSL/TLS 安全通道。

我也尝试搜索此错误,但在人们建议使用的所有帖子中 - SecurityProtocolType.Tls12 和 ServicePointManager.Expect100Continue = true。

我已经在我的代码中使用了这些,但我仍然看到错误。此外,如果我尝试使用 Postman 发出类似的请求,它也可以正常工作。

还有什么我想念的吗?

【问题讨论】:

    标签: c# rest restsharp


    【解决方案1】:

    找到了。 C# 要求在用户存储中导入证书。 我错过了这部分。 将 pfx 导入用户存储后,对于第一个请求,我被提示允许操作系统请求,后续请求完美运行。

    【讨论】:

      猜你喜欢
      • 2018-09-27
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 2018-04-22
      • 2012-10-30
      相关资源
      最近更新 更多