我正在尝试使用 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 发出类似的请求,它工作得很好。
我还缺少什么吗?
沧海一幻觉
相关分类