curl第三课 HTTPS交互
作者:互联网
项目:跟乐橙云访问,需要使用HTTPS协议
代码:
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;
}
标签:setopt,HTTPS,easy,pCurlHandle,第三课,curl,CURLOPT,size 来源: https://blog.51cto.com/fengyuzaitu/2379503