Lesson Ten HTTPS interaction libcurl

Scene
       with music orange cloud access, use HTTPS protocol


Additional configuration, otherwise an error is returned CURLE_UNSUPPORTED_PROTOCOL
1) upgrade to version 7.61.0 libcurl version
2) libcurl project properties pre-compiler adds USE_SCHANNEL USE_WINDOWS_SSPI
3) adding additional static library Crypt32.lib in property reference project, Wldap32.lib
   

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;
}

CommunicateWithServerUsingHTTPS CLeChengIPC :: int (const String & strPostData :: STD, const String & strUrl STD ::, :: String & strResponseData STD)
{
 the CURL = curl_easy_init to pCurlHandle * ();
 of curl_easy_setopt (pCurlHandle, CURLOPT_CUSTOMREQUEST, "the POST");
 of curl_easy_setopt (pCurlHandle, CURLOPT_URL to, strUrl.c_str ());
 of curl_easy_setopt (pCurlHandle, CURLOPT_WRITEFUNCTION to, WriteResponseBody); // set the callback
 curl_easy_setopt (pCurlHandle, CURLOPT_HEADER, 1) ; // save the information to the HTTP header strResponseData
 of curl_easy_setopt (pCurlHandle, CURLOPT_WRITEDATA, & strResponseData); // set the callback function parameters, obtaining feedback
 curl_easy_setopt (pCurlHandle, CURLOPT_TIMEOUT, 15) ; // receive data timeout setting, within 10 seconds if the received data is not finished, exit
 curl_easy_setopt (pCurlHandle, CURLOPT_MAXREDIRS, 1) ; // Find the number, to prevent finding too deep
 curl_easy_setopt (pCurlHandle, CURLOPT_CONNECTTIMEOUT, 5) ; // connection timeout, if this value is too short may not cause the data request to disconnect the
 curl_easy_setopt (pCurlHandle, CURLOPT_SSL_VERIFYPEER, false) ; // set to not verify the certificate and the HOST
 of curl_easy_setopt (pCurlHandle, CURLOPT_SSL_VERIFYHOST, to false);
 of curl_easy_setopt (pCurlHandle, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
 of curl_easy_setopt (pCurlHandle, CURLOPT_POSTFIELDS, strPostData.c_str ());
 CURLcode nRet = curl_easy_perform (pCurlHandle);
 curl_easy_cleanup (pCurlHandle);
 return nRet;
}

Note
enum {
  CURL_SSLVERSION_DEFAULT,
  CURL_SSLVERSION_TLSv1, / * * the 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 CURL_SSLVERSION_SSLv3 two macro definitions and can not be used, or returns an error CURLE_SSL_CONNECT_ERROR, we recommend the use of macros CURL_SSLVERSION_TLSv1_2

Revision
2019/7/8 use CURL_SSLVERSION_SSLv2 cause unusual problems recorded CURLE_SSL_CONNECT_ERROR


Guess you like

Origin blog.51cto.com/fengyuzaitu/2435300