我想使用RestSharp在一个明显没有有效ssl证书的服务器上启动REST请求,因为我遇到了错误:
底层连接已关闭:无法建立SSL/TLS安全通道的信任关系。
我找到了this question,但提供的解决方案即使看起来很简单也不起作用,我仍然会遇到错误。这是我的代码,有什么想法可能出了问题?
当我在
提前感谢, Frank
底层连接已关闭:无法建立SSL/TLS安全通道的信任关系。
我找到了this question,但提供的解决方案即使看起来很简单也不起作用,我仍然会遇到错误。这是我的代码,有什么想法可能出了问题?
private void Test_REST()
{
var client = new RestClient("https://online.gema.de");
// both versions not working
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
client.RemoteCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
// execute the request
IRestResponse response = client.Execute(GEMA_Authorize());
var content = response.Content; // raw content as string
}
private RestRequest GEMA_Authorize()
{
RestRequest request = new RestRequest("api/v1/authorize", Method.GET);
request.AddParameter("response_type", "token");
request.AddParameter("client_id", "test_client");
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
return request;
}
当我像这样编写回调函数:
.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) =>
{
return true;
};
当我在
return true;
处设置断点时,执行并没有停在那里,所以看起来回调函数从未被调用。有什么想法是什么可能出了问题?我对REST比较陌生,可能会错过一些关键内容。提前感谢, Frank
HttpWebRequest
,并且回调分配应该有效。client.RemoteCertificateValidationCallback =(sender,certificate,chain,sslPolicyErrors)=> true;
应该没问题。 - Alexey Zimarev