【问题标题】:RestSharp and ignoring errors in SSL certificateRestSharp 并忽略 SSL 证书中的错误
【发布时间】:2018-03-07 15:56:17
【问题描述】:

我正在尝试在显然没有有效 ssl 证书的服务器上使用 RestSharp 启动 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 比较陌生,所以我可能会错过一些重要的东西。

提前致谢,
弗兰克

【问题讨论】:

  • 客户端回调不是事件处理程序而是委托。当您需要使用= 时,为什么要为它们分配+=? RestSharp 没有 SSL 处理。它使用HttpWebRequest 并且回调分配应该可以工作。 client.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; 应该没问题。
  • 嗨,Alexey,非常感谢您的回复。我使用了 += 因为这是链接的 SO 问题使用它的方式。我用你的构造试过了,仍然没有成功,错误仍然存​​在。设置断点表明回调仍未被调用。
  • FWIW,我遇到了这个问题,从回调名称中找到了这个 Q,并使用了以下行:client.RemoteCertificateValidationCallback = (sender, certificate, chain, errors) => true;它对我来说“刚刚工作”。所以我无法帮助具体问题,但也许可以帮助找到它的其他人......

标签: rest ssl restsharp


【解决方案1】:

用法

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

为我工作。

【讨论】:

  • 这不再是 restsharp 107 中的选项
猜你喜欢
  • 2012-05-10
  • 2015-04-22
  • 2012-08-17
  • 1970-01-01
  • 2019-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多