【问题标题】:C++ and cUrl : how to get SSL error codesC++ 和 cUrl:如何获取 SSL 错误代码
【发布时间】:2012-02-13 07:52:25
【问题描述】:

我正在通过 SSL 建立与安全服务器的连接。 一切正常,我的 CAcertificate 通过

得到了很好的使用
retCode=curl_easy_setopt(handleCurl, CURLOPT_CAINFO, sSSLCertificate);
retCode=curl_easy_setopt(handleCurl, CURLOPT_SSL_VERIFYPEER, 1);

当我尝试管理 curl 错误时,我被卡住了。基本上我希望能够在 SSL 发生问题时得到通知(错误的 cacert.pem、未验证服务器身份等)。

当 CURLOPT_CAINFO 被赋予一个空的 CAcert 时,什么都不会发生,retCode 是可以的。

我试图在请求后获取信息:

res = curl_easy_getinfo(m_pHandleCurl, CURLINFO_SSL_VERIFYRESULT, &lSSLVerifyResult);

但它总是告诉我一切都很好。

我错过了什么?

【问题讨论】:

标签: c++ ssl curl error-handling


【解决方案1】:

添加到您的连接设置代码:

// Make sure this is NOT a stack variable! The buffer
// must be available through whole live of the connection
char buffer[CURL_ERROR_SIZE+1] = {};

retCode=curl_easy_setopt(handleCurl, CURLOPT_ERRORBUFFER, buffer);

然后,当您的连接结束时,检查缓冲区中的内容 - 您也应该能够看到有关 SSL 状态的一些提示。如果没有发生错误,它将为空。

如果您需要实际代码,数字 CURLcode 始终由 curl_easy_perform 返回,以便于处理。

如果您使用多句柄,请改用curl_multi_info_read。这是一个例子:

int u = 0;
if (CURLM_OK == curl_multi_perform(multi_, &u))
{
  int q = 0;
  CURLMsg *msg = NULL;
  while ((msg = curl_multi_info_read(multi_, &q)) != NULL)
  {
    if (msg->msg == CURLMSG_DONE)
    {
      CURL* easy = msg->easy_handle;
      CURLcode code = msg->data.result;
      // . . .
    }
  }
}

【讨论】:

  • 感谢您的提示,我刚刚尝试过并得到了一些结果:“错误设置证书验证位置:...”但我宁愿有一个错误号码或其他东西可以切换,就像那样例如:curl.haxx.se/libcurl/c/libcurl-errors.html
  • 而我在我的应用程序中使用 curl_multi_perform 并且它的错误代码不如 curl_easy_perform 有用。
  • 在这种情况下使用curl.haxx.se/libcurl/c/curl_multi_info_read.html返回的CURLMsg的“结果”字段
猜你喜欢
  • 1970-01-01
  • 2018-07-12
  • 2012-06-22
  • 2012-09-17
  • 1970-01-01
  • 2016-09-23
  • 1970-01-01
  • 2012-07-13
  • 2013-01-30
相关资源
最近更新 更多