libcurl第十课 HTTPS交互
作者:互联网
场景
跟乐橙云访问,需要使用HTTPS协议
额外配置,否则执行返回CURLE_UNSUPPORTED_PROTOCOL错误
1)libcurl版本升级到7.61.0版本
2)libcurl项目属性中预编译器添加USE_WINDOWS_SSPI USE_SCHANNEL
3)在引用项目的属性中添加额外的静态库Crypt32.lib,Wldap32.lib
代码
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);//设置回调函数
curl_easy_setopt(pCurlHandle, CURLOPT_HEADER, 1);//保存HTTP头部信息到strResponseData
curl_easy_setopt(pCurlHandle, CURLOPT_WRITEDATA, &strResponseData);//设置回调函数的参数,获取反馈信息
curl_easy_setopt(pCurlHandle, CURLOPT_TIMEOUT, 15);//接收数据时超时设置,如果10秒内数据未接收完,直接退出
curl_easy_setopt(pCurlHandle, CURLOPT_MAXREDIRS, 1);//查找次数,防止查找太深
curl_easy_setopt(pCurlHandle, CURLOPT_CONNECTTIMEOUT, 5);//连接超时,这个数值如果设置太短可能导致数据请求不到就断开了
curl_easy_setopt(pCurlHandle, CURLOPT_SSL_VERIFYPEER, false);//设定为不验证证书和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;
}
注意
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和CURL_SSLVERSION_SSLv3两个宏定义不能使用,否则返回错误CURLE_SSL_CONNECT_ERROR,建议使用CURL_SSLVERSION_TLSv1_2宏定义
修订
2019/7/8 使用CURL_SSLVERSION_SSLv2导致CURLE_SSL_CONNECT_ERROR异常问题记录
标签:SSLVERSION,HTTPS,libcurl,第十课,easy,pCurlHandle,CURL,curl,setopt 来源: https://blog.51cto.com/fengyuzaitu/2435300