從官網下載的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;
}
