C ++中的Curl-无法从HTTPS获取数据 [英] Curl in C++ - Can't get data from HTTPS
问题描述
当我尝试使用代码从HTTPS获取数据
When I trying to get data from HTTPS with code
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
string* NetIO::getUrlContent(string URL) {
CURL *curl;
CURLcode res;
string *s = new string("");
curl = curl_easy_init();
if (curl) {
//curl_easy_setopt(curl, CURLOPT_URL, URL);
curl_easy_setopt(curl, CURLOPT_URL, "https://ya.ru/");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, s);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
return s;
}
它无法接收HTTPS响应的内容并将其写入 s
变量- s
包含".但是,如果我将URL更改为HTTP,则此代码可以很好地工作,并且 s
包含页面的HTML代码.
It can't receive content of HTTPS response and write it to the s
variable - s
contains "". But if I change URL to HTTP - this code works very well and s
contains HTML code of the page.
curl_easy_setopt(curl, CURLOPT_URL, "http://tatarstan.shop.megafon.ru/");
如何解决此问题?
谢谢!❤
更新:如果我添加
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
比我的代码效果更好.为什么没有CURLOPT_SSL_VERIFYPEER = 0不能正常工作?
Than my code works well. Why it can't work without CURLOPT_SSL_VERIFYPEER=0?
我有一些Visual Studio解决方案.它包含2个项目-libcurl,该项目使用设置进行编译:
I have some Visual Studio solution. It contains 2 project - libcurl, that compiled with settings:
和第二个项目,这取决于libcurl项目:
and second project, that depends on libcurl project:
推荐答案
已解决!感谢@hank!
Solved! Thanks to @hank!
我的解决方法是:
1)在Google Chrome浏览器中打开您的网站,打开证书信息.
1) Open your site in Google Chrome, open certificate info.
2)导出它,并将其每个父证书导出为X.509.将Concat证书文件合并为一个证书文件-制作证书链.
2) Export it and each of his parent certs as X.509. Concat cert files into the one cert file - make cert chain.
3)将带有证书链的文件与代码一起使用
3) Use file with cert chain with the code
curl_easy_setopt(curl, CURLOPT_CAINFO, "c:\\Art\\Projects\\LifeSimLauncher\\MyStaticLib\\lifesim.biz.crt.cer");
这篇关于C ++中的Curl-无法从HTTPS获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!