且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

添加对libcurl的自签名SSL证书

更新时间:2021-10-12 05:51:35

要添加一个自签名证书,使用 CURLOPT_CAINFO

To add a self-signed certificate, use CURLOPT_CAINFO

要检索网站的SSL公共证书,使用

To retrieve the SSL public certificate of a site, use

openssl s_client -connect www.site.com:443 | tee logfile

该证书---- 按标记的部分---- BEGIN CERTIFICATE和结果
--- END CERTIFICATE ----

保存该证书到一个文件中,并使用卷曲的方式,像这样:

Save that certificate into a file, and use curl in a manner like so:

CURL* c = curl_easy_init();
curl_easy_setopt(c, CURLOPT_URL, "https://www.site.com");
curl_easy_setopt(c, CURLOPT_CAINFO, "/path/to/the/certificate.crt");
curl_easy_setopt(c, CURLOPT_SSL_VERIFYPEER, 1);
curl_easy_perform(c);
curl_easy_cleanup(c);