[英]Curl in C++ - Can't get data from HTTPS
當我嘗試使用代碼從 HTTPS 獲取數據時
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 代碼。
curl_easy_setopt(curl, CURLOPT_URL, "http://tatarstan.shop.megafon.ru/");
如何解決這個問題?
謝謝! ❤
更新:如果我添加
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
比我的代碼運行良好。 為什么沒有 CURLOPT_SSL_VERIFYPEER=0 就不能工作?
解決了! 感謝@hank!
我的解決辦法是:
1) 在谷歌瀏覽器中打開您的網站,打開證書信息。
2) 將它和他的每個父證書導出為 X.509。 將證書文件合並為一個證書文件 - 制作證書鏈。
3)使用帶有證書鏈的文件和代碼
curl_easy_setopt(curl, CURLOPT_CAINFO, "c:\\Art\\Projects\\LifeSimLauncher\\MyStaticLib\\lifesim.biz.crt.cer");
使用以下代碼:
curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.