我有以下代码可以连接到站点:
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;
}
Run Code Online (Sandbox Code Playgroud)
运行时出现错误:对等证书无法使用给定的 ca 证书进行身份验证
谷歌搜索后,我发现我必须添加以下行:
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
Run Code Online (Sandbox Code Playgroud)
但是现在我收到错误消息:ssl peer certificate or ssh remote key is not ok
我试过添加:
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2);
Run Code Online (Sandbox Code Playgroud)
但还是同样的错误。
我该如何解决这个问题??
编辑 我添加了详细日志记录:
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
Run Code Online (Sandbox Code Playgroud)
现在我在输出中看到以下内容: ssl 证书主题名称“名称或服务器”与目标主机不匹配
小智 5
服务器发送的 X.509 SSL 服务器证书无效。如果您真的想禁用 X.509 证书验证(请不要这样做),您应该将 CURLOPT_SSL_VERIFYHOST 设置为 0(默认为 2),以便在证书中包含的名称不匹配时要求 libcurl 不会失败您尝试连接的主机。如果你这样做,你可能不得不让 CURLOPT_SSL_VERIFYPEER 为 0,这意味着没有 X.509 PKI 验证。
| 归档时间: |
|
| 查看次数: |
15663 次 |
| 最近记录: |