I need to establish a connection via ssl socket at the following address : stream-api-integration.betfair.com (port 443), and read messages that come from the server using libcurl. I'm new to using this library. I tried to search for information on this issue, but I didn't find what I needed.
If the connection is established successfully, the following response should come from the server: {"op": "connection", "connection Id": "100-040320153715-29795"}.
How do I do this?
P.S. Meanwhile, I have no problems sending post requests to https using libcurl. If this helps in any way, I provide the code that successfully executes the post request. The problem is only with ssl sockets.
#include <curl/curl.h>
#include <string.h>
#include <sstream>
#include <iostream>
size_t writeToStream(char* buffer, size_t size, size_t nitems, std::ostream* stream)
{
size_t realwrote = size * nitems;
stream->write(buffer, static_cast<std::streamsize>(realwrote));
if (!(*stream)){
realwrote = 0;
}
return realwrote;
}
int main(int argc, char *argv[])
{
std::string url = "https://identitysso-cert.betfair.com/api/certlogin";
std::string acceptHeader = "Accept: application/json");
std::string appHeader = "X-Application: myappkey");
std::string contentTypeHeader = "Content-Type: application/x-www-form-urlencoded";
curl_slist* slist = nullptr;
slist = curl_slist_append(slist, acceptHeader.c_str());
slist = curl_slist_append(slist, appHeader.c_str());
slist = curl_slist_append(slist, contentTypeHeader.c_str());
std::stringstream result;
char errorBuffer[CURL_ERROR_SIZE];
CURL *curl = curl_easy_init();
if(curl != nullptr){
curl_easy_setopt(curl, CURLOPT_CAINFO, "../cacert.pem");
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_ALL);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
std::string certFilePath = "../client-2048.crt";
std::string keyFilePath = "../client-2048.key";
curl_easy_setopt(curl, CURLOPT_SSLCERT, certFilePath.c_str());
curl_easy_setopt(curl, CURLOPT_SSLKEY, keyFilePath.c_str());
std::string payload = "username=myusername&password=mypassword";
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeToStream);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
errorBuffer[0] = 0;
int responseCode = -1;
CURLcode curlResult = curl_easy_perform(curl);
if (curlResult == CURLE_OK){
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &responseCode);
std::cout<<"response code: "<<responseCode<<std::endl;
std::cout<<result.str()<<std::endl;
}
else{
std::cout<<"Failed:("<<std::endl;
throw std::runtime_error(errorBuffer);
}
}
return 0;
}