【发布时间】:2018-10-10 06:32:55
【问题描述】:
我正在尝试使用 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);
【问题讨论】:
-
你的系统有代理吗,stackoverflow.com/a/47675172/713789
-
没有 @AnirudhaGupta ,只是直接从 localhost 测试到 API。
-
查看@Shai 的回答,如果效果不好,可以在Postman 和HttpClient 中测试吗,如果显示相同,则需要在您的机器中安装该证书。这个答案应该有效。