ShadowScripter
ShadowScripter

Reputation: 7369

Getting no content from a HTTPS connection using CURL

I've been trying to learn more about CURL recently and finally managed to compile and install it properly as a static library for my test project. Eventually I'll move on to learning about posting forms and such.

I've successfully managed to connect and print out page content from http://www.google.se.

When connecting to a secure http page https://www.google.se I get an empty string as page content.

I'm using this to get information about the options.

I've tried the things from this answer, but it didn't work or I did it wrong.
I also tried turning off verifypeer and verifyhost (though I really want to practice safe solutions), but it didn't work either.

What do I need to do to make it work? Am I doing something wrong?


Here's the test code

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

using namespace std;

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp){
    ((string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

int main(){
  CURL *curl;
  CURLcode res;
  string readBuffer;

  curl = curl_easy_init();
  if(curl){
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.se");
    //curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, TRUE);

    //Doesn't seem to work
    curl_easy_setopt(curl, CURLOPT_CAINFO, "path\\cacert.pem");

    //Neither does this
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, FALSE);

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);

    cout<<readBuffer<<endl;

    system("pause");
  }
  return 0;
}

Update

So I got the error message from curl saying Unsupported protocol, which I'm guessing is what it says when SSL doesn't work. So I had to recompile it with SSL (which is odd, because I thought I did the first time) but...

I'm almost about to give up. Sigh. For some reason or other, now it gave me the error NMAKE: fatal error U1077 nasmw when making the SSL, even though I clearly gave it the right %PATH% to nasm. I followed the steps to the letter.

So I tried using the curl binaries of type libcurl, but I don't know how to link it properly in VC++ because the library files are unfamiliar to me.

I keep getting these linker errors when trying to compile the test project.

1>main.obj : error LNK2019: unresolved external symbol _curl_easy_cleanup referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol _curl_easy_strerror referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol _curl_easy_perform referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol _curl_easy_setopt referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol _curl_easy_init referenced in function _main

So frustrated... I wish I understood why it has to be so complicated.
I just want to use the library already!


Update 2

Ok... I managed to compile the curl library with SSL and reporting CURL_VERSION_SSL is enabled

curl_version_info_data * vinfo = curl_version_info(CURLVERSION_NOW);

if(vinfo->features & CURL_VERSION_SSL){
    cout<<"CURL: SSL enabled"<<endl;
}else{
    cout<<"CURL: SSL not enabled"<<endl;
}

//Prints out "CURL: SSL enabled"

but I'm still getting the same error message Unsupported protocol. I don't know what I'm doing wrong.

Upvotes: 4

Views: 6872

Answers (2)

ShadowScripter
ShadowScripter

Reputation: 7369

Alright, after I'd compiled the library for the nth time, everything seemed fine until I debugged it and it gave me heap errors, suggesting I made a mistake *twitch twitch* again.

Before starting a new compiling session I took another look around the curl.haxx and found an MSVC package. It made me gleeful, because I know how to handle those! I chose the static library, because that's what I wanted.

Though, not everything went smoothly, I had to add some additional linker dependencies for it to work properly.

namely

#pragma comment(lib, "curllib_static.lib")
#pragma comment(lib, "wldap32.lib")
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "ssleay32.lib")
#pragma comment(lib, "openldap.lib")
#pragma comment(lib, "libeay32.lib")

Put those in and used the certificate bundle from the other package I downloaded so that I could connect to HTTPS.

curl_easy_setopt(curl, CURLOPT_CAINFO, "_path_\ca-bundle.crt");

I am getting a few warnings though, warnings I've never seen before. They don't mess anything up, but it's got something to do with the PDB file "vc90", something I thought VC++ handled by itself.

Here's the code connecting to https://www.google.se (pragmas removed and added in linker dependencies under project properties) after finally having a proper CURL library.

Here's the MSVC package (SSL Enabled). You can use one of the scripts (perl or VBscript) from this package to make the certificate bundle for you.

Oh, also, if you're using a static library like me, you have to add the preprocessor definition CURL_STATICLIB

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

using namespace std;

static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp){
    ((string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}

int main(){
  CURL *curl;
  CURLcode res;
  string readBuffer;

  curl = curl_easy_init();
  if(curl){
    curl_easy_setopt(curl, CURLOPT_URL, "https://www.google.se");
    curl_easy_setopt(curl, CURLOPT_CAINFO, "C:\\libcurl_static\\ca-bundle.crt");
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);

    cout<<readBuffer<<endl;

    system("pause");
  }
  return 0;
}

Was really my fault, I should've checked the description on the download wizard they have. I only downloaded the latest source from the archives (7.27.0) because I thought I had to compile it myself to get what I wanted.

This whole thing is embarrassing. I learned a lot from it though.

Upvotes: 2

Daniel Stenberg
Daniel Stenberg

Reputation: 58084

Use -v to inspect the full response. It is probably a redirect. Also, Google is known to block requests done with curl's default user-agent...

Upvotes: 1

Related Questions