📜 ⬆️ ⬇️

use libcurl to access https

Sometimes you need to get some data from the report pages and save it locally. there were no problems for normal pages, but I wanted to get an account balance from a local provider, and access to this page is only via https.


for this, we had to use the libcurl library (http://curl.haxx.se/libcurl/)
somehow it did without it. eight)

Here is an example of what I did
')
/*
* cURL HTTPS
*/



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

// , cURL
std :: string curlBuffer ;
size_t curlWriteFunc ( char * data, size_t size, size_t nmemb, std :: string * buffer )
{
size_t result = 0 ;

if ( buffer ! = NULL )
{
buffer - > append ( data, size * nmemb ) ;
result = size * nmemb ;
}
return result ;
}



//
//
//
int main ( int argc, char * argv [ ] )
{
// ( login screen)
const char * url = "_/cgi-bin/utm5/aaa5" ;
//
const char * urlPOST = "login=&password=&cmd=login" ;

//
char curlErrorBuffer [ CURL_ERROR_SIZE ] ;

CURL * curl = curl_easy_init ( ) ;
if ( curl ) {
//
curl_easy_setopt ( curl, CURLOPT_ERRORBUFFER, curlErrorBuffer ) ;

// URL...
curl_easy_setopt ( curl, CURLOPT_URL, url ) ;
// "Location:" HTTP
curl_easy_setopt ( curl, CURLOPT_FOLLOWLOCATION, 1 ) ;
//
curl_easy_setopt ( curl, CURLOPT_SSL_VERIFYPEER, O ) ;
// POST
curl_easy_setopt ( curl, CURLOPT_POST, 1 ) ;
// POST
curl_easy_setopt ( curl, CURLOPT_POSTFIELDS, urlPOST ) ;
// , cURL
curl_easy_setopt ( curl, CURLOPT_WRITEDATA, & curlBuffer ) ;
curl_easy_setopt ( curl, CURLOPT_WRITEFUNCTION, curlWriteFunc ) ;

//
CURLcode curlResult = curl_easy_perform ( curl ) ;
//
curl_easy_cleanup ( curl ) ;

if ( curlResult == CURLE_OK )
{
std :: cout << curlBuffer << std :: endl ;
return ( 0 ) ;
} else {
std :: cout << "(" << curlResult << "): " << curlErrorBuffer << std :: endl ;
return ( - 1 ) ;
}

}
return 0 ;
}


That's how my acquaintance with this library went.
mmm ... probably you need to do an automatic receipt and installation of the certificate, while wondering whether it is worth it at all. eight)
better while downloading a wrapper for with ++

______________________
The text was prepared in the Habr Editor from © SoftCoder.ru

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


All Articles