如何使libcurl支持https
从官网下载的libcurl库只能支持http,如果要支持https,需要用libcurl的源码和openssl重新编译。具体的过程我是根据这个帖子来进行的。http://www.cnblogs.com/openiris/p/3812443.html,很详细,我按照这个步骤也是成功的。如果想要直接编好的库,可以直接找我要。
要在使用libcurl时加入对证书的设置,能发起https的请求。
extern "C" __declspec(dllexport) int __cdecl https_post(const char* strUrl,char *szPost,const char * pCaPath)
{
CURL *curl;
CURLcode res;
res_buf = "";
curl = curl_easy_init(); //初始化
if(curl&&strUrl)
{
curl_easy_setopt(curl,CURLOPT_URL,strUrl); //设置url地址
if(szPost)
{
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,szPost); //设置post参数
}
curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,write_func); //设置回调函数
curl_easy_setopt(curl,CURLOPT_WRITEDATA,&res_buf); //设置写数据
if(NULL == pCaPath)
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);//设定为不验证证书和HOST
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
}
else
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
}
res = curl_easy_perform(curl); //执行
if(res == CURLE_OK)
{
if(m_json)
{
delete m_json;
m_json = NULL;
}
m_json = new char[strlen(res_buf.c_str())+1];
strcpy(m_json,Utf8toAnsi(res_buf.c_str()));;
curl_easy_cleanup(curl);
return 1;
}
return -1;
}
return -1;
}

浙公网安备 33010602011771号