为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
慕侠2389804 2023-08-20 14:53:17
我正在尝试使用 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 发出类似的请求,它工作得很好。我还缺少什么吗?
查看完整描述

1 回答

?
沧海一幻觉

TA贡献1824条经验 获得超5个赞

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



查看完整回答
反对 回复 2023-08-20
  • 1 回答
  • 0 关注
  • 232 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信