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

cla*_*ica 2 c# asp.net ssl restsharp

我正在尝试使用 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;
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

小智 6

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

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

相关资源: