Libcurl lesson 10 HTTPS interaction
Scene
To visit with Le Orange Cloud, HTTPS protocol is required.
Additional configuration, otherwise the execution returns a CURLE_UNSUPPORTED_PROTOCOL error
1) upgrade libcurl version to 7.61.0 version
2) add USE_WINDOWS_SSPI and USE_SCHANNEL to the precompiler in the libcurl project properties
3) add additional static library Crypt32.lib,Wldap32.lib to the properties of the reference project
Code
Size_t CLeChengIPC::WriteResponseBody (void * ptr, size_t size, size_t nmemb, void * userData)
{
Std::string* pStrBuffer = (std::string*) userData
Size_t nLen = size * nmemb
PStrBuffer- > append ((char*) ptr, nLen)
Return nLen
}
Int CLeChengIPC::CommunicateWithServerUsingHTTPS (const std::string & strPostData, const std::string & strUrl, std::string & strResponseData)
{
CURL * pCurlHandle = curl_easy_init ()
Curl_easy_setopt (pCurlHandle, CURLOPT_CUSTOMREQUEST, "POST")
Curl_easy_setopt (pCurlHandle, CURLOPT_URL, strUrl.c_str ())
Curl_easy_setopt (pCurlHandle, CURLOPT_WRITEFUNCTION, WriteResponseBody); / / set callback function
Curl_easy_setopt (pCurlHandle, CURLOPT_HEADER, 1); / / Save HTTP header information to strResponseData
Curl_easy_setopt (pCurlHandle, CURLOPT_WRITEDATA, & strResponseData); / / set the parameters of the callback function to get feedback information
Curl_easy_setopt (pCurlHandle, CURLOPT_TIMEOUT, 15); / / timeout setting when receiving data. If the data is not received within 10 seconds, exit directly.
Curl_easy_setopt (pCurlHandle, CURLOPT_MAXREDIRS, 1); / / search times to prevent search too deep
Curl_easy_setopt (pCurlHandle, CURLOPT_CONNECTTIMEOUT, 5); / / connection timeout. If this value is set too short, it may disconnect if the data is not requested.
Curl_easy_setopt (pCurlHandle, CURLOPT_SSL_VERIFYPEER, false); / / set to not validate certificates and HOST
Curl_easy_setopt (pCurlHandle, CURLOPT_SSL_VERIFYHOST, false)
Curl_easy_setopt (pCurlHandle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1)
Curl_easy_setopt (pCurlHandle, CURLOPT_POSTFIELDS, strPostData.c_str ())
CURLcode nRet= curl_easy_perform (pCurlHandle)
Curl_easy_cleanup (pCurlHandle)
Return nRet
}
Be careful
Enum {
CURL_SSLVERSION_DEFAULT
CURL_SSLVERSION_TLSv1, / * TLS 1.x * /
CURL_SSLVERSION_SSLv2
CURL_SSLVERSION_SSLv3
CURL_SSLVERSION_TLSv1_0
CURL_SSLVERSION_TLSv1_1
CURL_SSLVERSION_TLSv1_2
CURL_SSLVERSION_TLSv1_3
CURL_SSLVERSION_LAST / * never use, keep last * /
}
CURL_SSLVERSION_SSLv2 and CURL_SSLVERSION_SSLv3 macro definitions cannot be used, otherwise an error CURLE_SSL_CONNECT_ERROR is returned. It is recommended to use CURL_SSLVERSION_TLSv1_2 macro definitions.
Revision
Record of CURLE_SSL_CONNECT_ERROR exception problems caused by the use of CURL_SSLVERSION_SSLv2 in 2019-7-8