gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:41:28 29 4
gpt4 key购买 nike

我正在尝试使用 RestSharp 在我的本地机器上测试 API 调用,代码如下...

        var client = new RestClient("https://[API URL]");

var request = new RestRequest( Method.POST);
request.AddParameter("session", this, ParameterType.RequestBody);

IRestResponse<SessionOut> response = client.Execute<SessionOut>(request);
return response.Data.session.id;

response 中,我收到一条错误消息,告诉我请求已中止,因为它“无法创建 SSL/TLS 安全通道”。

这是否意味着我需要尝试设置 https://localhost而不是 http://localhost以便在 https://地址调用 API?

更新

根据@Shai_Aharoni 在下方的回答,我已将我的代码更新为以下内容。但是,我仍然遇到同样的错误。

        string pathToYourClientCert = Path.Combine( AppDomain.CurrentDomain.BaseDirectory, "[my certificate file");
var client = new RestClient("[API URL]/");
client.ClientCertificates = new X509CertificateCollection();
client.ClientCertificates.Add(new X509Certificate(pathToYourClientCert));

var request = new RestRequest( Method.POST);
request.AddParameter("session", this, ParameterType.RequestBody);

IRestResponse<SessionOut> response2 = client.Execute<SessionOut>(request);

最佳答案

尝试将此添加到您的代码中:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

相关资源:

关于c# - RestSharp 请求中止 - 无法创建 SSL/TLS 安全通道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52733988/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com