我有以下代码可以连接到站点:
int main()
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "https://192.168.200.115:8080/appliances");
curl_easy_setopt(curl, CURLOPT_USERNAME, "myusername");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "mypassword");
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// Perform the request, res will get the return code
res = curl_easy_perform(curl);
// Check for errors
if(res != CURLE_OK)
{
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
//always cleanup
curl_easy_cleanup(curl);
}
return 0;
}
运行时出现错误: 无法使用给定的 ca 证书对对等证书进行身份验证
谷歌搜索后,我发现我必须添加以下行:
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
但是现在我得到了错误: ssl peer certificate or ssh remote key was not ok
我试过添加:
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2);
但仍然是同样的错误。
我该如何解决这个问题?
编辑 我添加了详细的日志记录:
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
现在我在输出中看到以下内容: ssl certificate subject name ‘name or server’ does not match target host
原文由 Harry Boy 发布,翻译遵循 CC BY-SA 4.0 许可协议
服务器发送的 X.509 SSL 服务器证书无效。如果您真的想禁用 X.509 证书验证(请不要这样做),您应该将 CURLOPT_SSL_VERIFYHOST 设置为 0(默认为 2),以便在证书中包含的名称不匹配时要求 libcurl 不失败您尝试连接的主机。如果你这样做,你可能不得不让 CURLOPT_SSL_VERIFYPEER 为 0,这意味着没有 X.509 PKI 验证。