RestSharp和忽略SSL证书错误

5
我想使用RestSharp在一个明显没有有效ssl证书的服务器上启动REST请求,因为我遇到了错误:
底层连接已关闭:无法建立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

1
客户端回调不是事件处理程序,而是委托。当您需要使用“=”时,为什么要使用“+=”进行分配?RestSharp没有SSL处理。它使用HttpWebRequest,并且回调分配应该有效。 client.RemoteCertificateValidationCallback =(sender,certificate,chain,sslPolicyErrors)=> true; 应该没问题。 - Alexey Zimarev
嘿,Alexey,非常感谢您的回复。我使用+=是因为这是链接的SO问题中使用的方式。我尝试了您的构造,但仍然没有成功,错误仍然存在。设置断点会发现回调仍未被调用。 - Aaginor
1
就我个人而言,我曾经遇到过这个问题,通过回调名称找到了这个问题,并使用了以下代码: client.RemoteCertificateValidationCallback = (sender, certificate, chain, errors) => true; 对我来说,它“只是起作用了”。所以我无法帮助具体的问题,但或许可以帮助其他人。 - mj2008
1个回答

3

使用

client.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;

对我有用。


1
在 RestSharp 107 中,这不再是一个选项。 - Kevin
1
它仅存在于RestClientOptions对象中,而不再存在于RestClient本身中。 - Marcel Grüger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接