0

I downloaded the source code of Curl and built the library (libcurl.lib). Following is the code to read from the site and dump the contents. The code works well for http sites and fails for https. I tried downloading openssl libraries but unable to link them as more linker errors are thrown.

What is the best solution to handle this?

#include "stdafx.h"
#include <iostream>
#include <string>
#include <curl/curl.h>


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;
}

int main(void)
{
    CURL *curl;
    CURLcode res;
    std::string readBuffer;
    std::string curl_url = "https://www.example.com/";
    curl = curl_easy_init();
    if(curl) {
        curl_easy_setopt(curl, CURLOPT_URL, curl_url.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
        res = curl_easy_perform(curl);
        if (res != CURLE_OK) {
            std::cout << "Error from cURL: " << curl_easy_strerror(res) << std::endl;
        }
        curl_easy_cleanup(curl);
        std::cout << "Finished reading from the website" << std::endl;
        std::cout << readBuffer << std::endl;
    }
    return 0;
}
nsivakr
  • 1,565
  • 2
  • 25
  • 46
  • Not sure about c++ but in PHP we add a curl option to disable peer verification: https://stackoverflow.com/questions/4372710/php-curl-https – David Feb 12 '19 at 13:57
  • 1
    How does it fail, exactly? When you built your libcurl, did you enable HTTPS? – Daniel Stenberg Feb 12 '19 at 14:02
  • Following are the pre-processes I have enabled while building curl. CURL_STATICLIB, USE_IPV6, USE_OPENSSL, USE_SSLEAY – nsivakr Feb 12 '19 at 14:18
  • When I include the USE_OPENSSL, the library (libcurl.lib) simply fails to link with any of the openssl libraries. When I exclude USE_OPENSSL, unable to access any of the https sites. – nsivakr Feb 12 '19 at 14:19
  • I know this is too simple as many of you have done, but not sure of the right approach. – nsivakr Feb 12 '19 at 14:20
  • Without SSL enabled in the build, you won't be able to transfer HTTPS. You need to fix that first. – Daniel Stenberg Feb 12 '19 at 21:57
  • What is the command line you applied to build libcurl? – Oleg Feb 12 '19 at 22:39
  • I used Visual C++ IDE and built using Lib Release with X64 option. – nsivakr Feb 12 '19 at 23:17
  • Does curl have Visual C++ project file? Or you created the new project and imported all files? We need to understand what was the command line to check which options did you include into the build. – Oleg Feb 12 '19 at 23:26
  • Yes. Curl have project profile for Visual C++. It is under the directory curl-7.64.0\projects\Windows – nsivakr Feb 12 '19 at 23:31

0 Answers0