using libcurl to access https
sometimes you need to get some data from the report pages and save it locally. there were no problems for ordinary pages, but I wanted to get an account balance from a local provider, and access to this page is only via https.
To do this, I had to use the libcurl library (http://curl.haxx.se/libcurl/)
somehow I could do without it before. 8)
here is an example of what I got like this: my acquaintance with this library passed. mmm ... probably you need to do the automatic receipt and installation of the certificate, while I wonder if it is worth doing it at all. 8) it’s better while I download the wrapper for c ++ ______________________
To do this, I had to use the libcurl library (http://curl.haxx.se/libcurl/)
somehow I could do without it before. 8)
here is an example of what I got like this: my acquaintance with this library passed. mmm ... probably you need to do the automatic receipt and installation of the certificate, while I wonder if it is worth doing it at all. 8) it’s better while I download the wrapper for c ++ ______________________
/*
* использование cURL для доступа к HTTPS
*/
#include
#include
#include
// функция, вызываемая 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;
}