#include "LibcurlHttp.h"
#ifdef _WIN64
#ifdef _DEBUG
#pragma comment(lib,"../Lib64/libcurl_debug.lib")
#else
#pragma comment(lib,"../Lib64/libcurl.lib")
#endif
#else
#ifdef _DEBUG
#pragma comment(lib,"../Lib/libcurl_debug.lib")
#else
#pragma comment(lib,"../Lib/libcurl.lib")
#endif
#endif
CLibcurlHttp::CLibcurlHttp()
:m_bDebug(false)
, m_OutTime(10)
{
}
CLibcurlHttp::~CLibcurlHttp()
{
}
static int OnDebug(CURL *, curl_infotype itype, char * pData, size_t size, void *)
{
if (itype == CURLINFO_TEXT)
{
printf("[TEXT]%s\n", pData);
}
else if (itype == CURLINFO_HEADER_IN)
{
printf("[HEADER_IN]%s\n", pData);
}
else if (itype == CURLINFO_HEADER_OUT)
{
printf("[HEADER_OUT]%s\n", pData);
}
else if (itype == CURLINFO_DATA_IN)
{
printf("[DATA_IN]%s\n", pData);
}
else if (itype == CURLINFO_DATA_OUT)
{
printf("[DATA_OUT]%s\n", pData);
}
return 0;
}
static size_t OnWriteData(void* buffer, size_t size, size_t nmemb, void* lpVoid)
{
std::string* str = dynamic_cast<std::string*>((std::string *)lpVoid);
if (NULL == str || NULL == buffer)
{
return -1;
}
char* pData = (char*)buffer;
str->append(pData, size * nmemb);
return nmemb;
}
/* libcurl write callback function */
size_t WriteData(void *ptr, size_t size, size_t nMenmb, FILE *stream)
{
size_t written = fwrite(ptr, size, nMenmb, stream);
return written;
//在这里可以把下载到的数据以追加的方式写入文件
//FILE* fp = NULL;
//fopen_s(&fp, "c:\\test.dat", "ab+");//一定要有a, 否则前面写入的内容就会被覆盖了
//size_t nWrite = fwrite(ptr, nSize, nmemb, fp);
//fclose(fp);
//return nWrite;
}
int CLibcurlHttp::Post(const string & strUrl, const string & strPost, string & strResponse, const bool bIsJosn)
{
CURLcode res;
//初始化curl,生成CURL *curl指针
CURL *curl = curl_easy_init();
if (nullptr == curl)
{
return CURLE_FAILED_INIT;
}
if (m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); //调试信息
curl_easy_setopt(curl,CURLOPT_DEBUGFUNCTION, OnDebug);
}
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
curl_easy_setopt(curl, CURLOPT_POST, true);
/* struct curl_slist *curl_slist_append(struct curl_slist * list, const char * string);
@ 添加Http消息头
@ 属性string:形式为name+": "+contents
*/
curl_slist *pList = nullptr;
if (bIsJosn) //设置http发送的内容类型未JSON
{
pList = curl_slist_append(NULL, "Content-Type:application/json;charset=UTF-8");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, pList);
}
/* CURLcode curl_easy_setopt(CURL *handle, CURLoption option, parameter);
@ 设置属性及常用参数
*/
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false); //证书验证
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false); //ssl算法验证
curl_easy_setopt(curl, CURLOPT_USERNAME, "test");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "1234");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&strResponse);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); //是否抓取跳转后的页面
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, m_OutTime);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, m_OutTime);
res = curl_easy_perform(curl);
if (pList)
{
curl_slist_free_all(pList);
}
curl_easy_cleanup(curl);
return res;
}
int CLibcurlHttp::Get(const string & strUrl, string & strResponse)
{
CURLcode res;
CURL *curl = curl_easy_init();
if (nullptr == curl)
{
return CURLE_FAILED_INIT;
}
if (m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str()); //访问的URL
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL); //
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData); //数据回调函数
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&strResponse); //数据回调参数,一般为buffer或者文件fd
/*
* 当多个线程都使用超时处理的时候,同时主线程中有sleep或是wait等操作。
* 如果不设置这个选项,libcurl将会发信号打断这个wait从而导致程序退出。
*/
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, m_OutTime);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, m_OutTime);
//https
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
curl_easy_setopt(curl, CURLOPT_CAINFO, "D:\\cert\\PC-12.crt");
curl_easy_setopt(curl, CURLOPT_USERNAME, "test");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "1234");
/* CURLcode curl_easy_perform(CURL *handle);
@ 开始下载
*/
if (res = curl_easy_perform(curl), CURLE_OK != res)
{
printf("%s\n", curl_easy_strerror(res));
}
/* void curl_easy_cleanup(CURL * handle);
@ 释放CURL *curl指针
*/
curl_easy_cleanup(curl);
/* void curl_global_cleanup(void);
@ 初始化libcurl,全局也只需调一次
*/
//curl_global_cleanup();
return res;
}
int CLibcurlHttp::Posts(const string & strUrl, const string & strPost, string & strResponse, const char * pCaPath)
{
CURLcode res;
CURL *curl = curl_easy_init();
if (nullptr == curl)
{
return CURLE_FAILED_INIT;
}
if (m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, strPost.c_str());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
if (nullptr == pCaPath)
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
}
else
{
//缺省情况就是PEM,所以无需设置,另外支持DER
//curl_easy_setopt(curl,CURLOPT_SSLCERTTYPE,"PEM");
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
}
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, m_OutTime);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, m_OutTime);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return res;
}
int CLibcurlHttp::Gets(const string & strUrl, string & strResponse, const char * pCaPath)
{
CURLcode res;
CURL *curl = curl_easy_init();
if (nullptr == curl)
{
return CURLE_FAILED_INIT;
}
if (m_bDebug)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
}
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, OnWriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&strResponse);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
if (nullptr == pCaPath)
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
}
else
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
curl_easy_setopt(curl, CURLOPT_CAINFO, pCaPath);
}
curl_easy_setopt(curl, CURLOPT_USERNAME, "test");
curl_easy_setopt(curl, CURLOPT_PASSWORD, "1234");
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, m_OutTime);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, m_OutTime);
res = curl_easy_perform(curl);
if (CURLE_OK != res)
{
curl_easy_cleanup(curl);
printf("%s\n", curl_easy_strerror(res));
return -1;
}
curl_easy_cleanup(curl);
return res;
}
int CLibcurlHttp::DownloadFile(const string & strUrl, const string & strFile)
{
FILE *fp;
CURL *curl = curl_easy_init();
CURLcode res;
if (nul