Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Curl simple https request returns nothing C++

Tags:

c++

curl

I'm using Visual Studio C++ 2010 and it's working just fine with cURL but the problem is that https requests returns nothing. instead of showing the output:

#include "StdAfx.h"
#include <stdio.h>
#include <curl/curl.h>
#include <conio.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.com");
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
  }
  _getch();
  return 0;
}

This code for example, it's just a https request to Google, but it returns nothing just because it starts with https. and if I take away the "s" of https, it works just fine: "http://www.google.com.br" shows the result normally. what am I missing here? I'm using the example from the cURL. I tried with other websites and same thing happened. :/ like https://www.facebook.com

Btw also if you guys know how do I store the webpage content in a string, I would be glad to know.

Thanks in advance. :)

like image 743
Grego Avatar asked Jul 17 '26 08:07

Grego


1 Answers

This simple example works for me:

#include <stdio.h>
#include <curl/curl.h>

int main(void)
{
  CURL *curl;
  CURLcode res;

  curl = curl_easy_init();
  if(curl) {
    /* First set the URL that is about to receive our POST. This URL can
       just as well be a https:// URL if that is what should receive the
       data. */
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.co.uk");

    /* Perform the request, res will get the return code */
    res = curl_easy_perform(curl);

    /* always cleanup */
    curl_easy_cleanup(curl);
  }
  return 0;
}

I got the source code and built the library long time ago but I'm sure I enabled the SSL support before compiling the sources. Make sure you have OpenSSL installed and (in case you're working under Linux) you have the PKG_CONFIG_PATH variable initialized properly. These are two options you specify when executing the configure script.

  --with-ssl=PATH         Where to look for OpenSSL, PATH points to the SSL
                          installation (default: /usr/local/ssl); when
                          possible, set the PKG_CONFIG_PATH environment
                          variable instead of using this option

  --without-ssl           disable OpenSSL

I hope it helps.

If you're using Windows, this post can be also useful for you:

Building libcurl with SSL support on Windows

like image 132
user1192525 Avatar answered Jul 21 '26 08:07

user1192525



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!