繁体   English   中英

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

[英]RestSharp request aborted - could not create SSL/TLS secure channel

我正在尝试使用 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;

相关资源:

嗯...在调用 HTTPS 端点之前,您需要完成几个步骤。

1) 确保您的服务器支持 HTTPS 端点(即:可以访问 URL https://[APIURL]

2) 在执行 HTTPS 调用的机器上安装了有效的服务器(api 服务器)证书。

3) 将证书添加到您的 RestSharp 客户端。 类似于这样的事情:

string pathToYourClientCert = "cer/cert.cer";
client.ClientCertificates.Add(new X509Certificate(pathToYourClientCert));

希望这可以帮助...

请按照以下步骤操作: https : //docs.microsoft.com/en-us/xamarin/android/app-fundamentals/http-stack?tabs=macos

还要将此添加到 MainActivity.cs -> OnCreate -> 加载应用程序之前的代码中:

ServicePointManager.SecurityProtocol |= SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

我必须同时为我的 API 请求在 Xamarin Forms 中工作!

确保您的证书不是自签名!!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM