我正在尝试使用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);
发布于 2018-10-18 10:58:38
尝试将以下代码添加到代码中:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
相关资源:
发布于 2018-10-10 14:47:00
好吧..。在调用HTTPS端点之前,您需要完成几个步骤。
1)确保您的服务器支持HTTPS终结点(即:可以访问URL )。
2)在执行HTTPS调用的机器上安装有效的服务器( api服务器)证书。
3)将证书添加到RestSharp客户端。类似于下面的内容:
string pathToYourClientCert = "cer/cert.cer";
client.ClientCertificates.Add(new X509Certificate(pathToYourClientCert));
希望这能帮到你。
发布于 2021-04-27 18:01:19
按照以下步骤操作: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;
为了在Xamarin表单中工作,我必须完成这两个API请求!
确保您的证书不是自签名的!
https://stackoverflow.com/questions/52733988
复制相似问题