Retrieving content from an HTTPS connection using CURL

I recently tried to learn more about CURL and finally managed to compile and install it as a static library for my test project. In the end, I will move on to studying forms of publication, etc.

I was able to successfully install and print the contents of the page from http://www.google.se .

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

I use this to get parameter information.

I tried things from this answer , but it did not work, or I did it wrong.
I also tried disabling verifypeer and verifyhost (although I really want to practice secure solutions), but it does not work either.

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


Here is 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 an error from curl saying the Unsupported protocol , which I assume is what it says when SSL is down. So I had to recompile it using SSL (which is strange because I thought I did it the first time), but ...

I'm almost going to give up. Sigh. For one reason or another, now it gave me the NMAKE: fatal error U1077 nasmw when creating SSL, although I clearly gave it% PATH% to nasm. I followed the steps to the letter.

So I tried to use curl binaries like libcurl , but I don’t know how to properly link it in VC ++, because the library doesn’t know the files.

I keep getting these linker errors when trying to compile a 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 upset ... I would like to understand why this should be so difficult. I just want to use the library already!


Update 2

Ok ... I managed to compile the curl library with SSL and report CURL_VERSION_SSL

 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 still get the Unsupported protocol error message. I do not know what I am doing wrong.

+4
source share
2 answers

Well, after I compiled the library for the nth time, everything seemed fine until I debugged it, and it gave me heap errors, assuming I made the * twitch twitch * error again.

Before starting a new compilation session, I looked again at curl.haxx and found the MSVC package. It made me happy because I know how to handle it! I chose a static library because that is what I wanted.

Although not everything went smoothly, I had to add some additional linker dependencies to make it work correctly.

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 them and use the certificate package from another package I downloaded so that I can connect to HTTPS.

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

I get a few warnings, although warnings I have never seen before. They didn't mess up anything, but it had something to do with the PDA file "vc90", something that I thought VC ++ was processed on its own.

Here is the code connecting to https://www.google.se (pragmas removed and added depending on the linker in the project properties) after finally having the proper CURL library.

Here's the MSVC package (SSL is enabled). You can use one of the scripts (perl or VBscript) from this package to make a set of certificates for you.

Oh, also, if you use a static library like me, you need to add the CURL_STATICLIB preprocessor 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; } 

Am I really to blame, I had to check the description in the download wizard that they have. I only downloaded the last source from the archives (7.27.0) because I thought I had to compile it myself in order to get what I wanted.

All this is awkward. However, I learned a lot from him.

+2
source

Use -v to check for a complete answer. This is probably a redirect. In addition, Google, as you know, blocks requests made using the default freezing user agent ...

+1
source

Source: https://habr.com/ru/post/1437480/


All Articles