Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
862 views
in Technique[技术] by (71.8m points)

visual studio - Curl in C++ - Can't get data from 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;
}

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/");

How to fix this issue?

Thank you! ?

UPDATE: If I add

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);

Than my code works well. Why it can't work without CURLOPT_SSL_VERIFYPEER=0?

I have some Visual Studio solution. It contains 2 project - libcurl, that compiled with settings: enter image description here enter image description here enter image description here

and second project, that depends on libcurl project: enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Solved! Thanks to @hank!

My solution is:

1) Open your site in Google Chrome, open certificate info.

2) Export it and each of his parent certs as X.509. Concat cert files into the one cert file - make cert chain.

3) Use file with cert chain with the code

curl_easy_setopt(curl, CURLOPT_CAINFO, "c:\Art\Projects\LifeSimLauncher\MyStaticLib\lifesim.biz.crt.cer");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...