curllib 头文件 及静态库 下载
链接:https://pan.baidu.com/s/1JSds3Kl9sQjVYy9F8piC5Q
提取码:9dk0
支持ssl 的版本
链接:https://pan.baidu.com/s/1YXyzzAmqIHRmelMjhNXn9A
提取码:xw5h
.h 文件
#include <curl/curl.h>
static size_t receive_data(void *contents, size_t size, size_t nmemb,
void *stream);
// HTTP 下载文件的回掉函数
static size_t writedata2file(void *ptr, size_t size, size_t nmemb,
FILE *stream);
// 文件下载接口
static int download_file(const char* url,
const char outfilename[FILENAME_MAX]);
// http get 请求
static CURLcode HttpGet(const std::string & strUrl,
std::string & strResponse, int nTimeout);
// htpp post 请求
static CURLcode HttpPost(const std::string & strUrl, std::string szJson,
std::string & strResponse, int nTimeout);
.cpp
size_t TForm1::receive_data(void *contents, size_t size, size_t nmemb, void *stream){
string *str = (string*)stream;
(*str).append((char*)contents, size*nmemb);
return size * nmemb;
}
size_t TForm1::writedata2file(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
int TForm1::download_file(const char* url, const char outfilename[FILENAME_MAX]){
CURL *curl;
FILE *fp;
CURLcode res;
/* 调用curl_global_init()初始化libcurl */
res = curl_global_init(CURL_GLOBAL_ALL);
if (CURLE_OK != res)
{
printf("init libcurl failed.");
curl_global_cleanup();
return -1;
}
/* 调用curl_easy_init()函数得到 easy interface型指针 */
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename,"wb");
/* 调用curl_easy_setopt()设置传输选项 */
res = curl_easy_setopt(curl, CURLOPT_URL, url);
if (res != CURLE_OK)
{
fclose(fp);
curl_easy_cleanup(curl);
return -1;
}
/* 根据curl_easy_setopt()设置的传输选项,实现回调函数以完成用户特定任务 */
res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writedata2file);
if (res != CURLE_OK){
fclose(fp);
curl_easy_cleanup(curl);
return -1;
}
/* 根据curl_easy_setopt()设置的传输选项,实现回调函数以完成用户特定任务 */
res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
if (res != CURLE_OK)
{
fclose(fp);
curl_easy_cleanup(curl);
return -1;
}
res = curl_easy_perform(curl);
// 调用curl_easy_perform()函数完成传输任务
fclose(fp);
/* Check for errors */
if(res != CURLE_OK){
fprintf(stderr, "curl_easy_perform() failed: %s
",curl_easy_strerror(res));
curl_easy_cleanup(curl);
return -1;
}
/* always cleanup */
curl_easy_cleanup(curl);
// 调用curl_easy_cleanup()释放内存
}
curl_global_cleanup();
return 0;
}
CURLcode TForm1::HttpGet(const std::string & strUrl, std::string & strResponse,int nTimeout){
CURLcode res;
CURL* pCURL = curl_easy_init();
if (pCURL == NULL) {
return CURLE_FAILED_INIT;
}
curl_easy_setopt(pCURL, CURLOPT_URL, strUrl.c_str());
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(pCURL, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(pCURL, CURLOPT_TIMEOUT, nTimeout);
curl_easy_setopt(pCURL, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(pCURL, CURLOPT_WRITEFUNCTION, receive_data);
curl_easy_setopt(pCURL, CURLOPT_WRITEDATA, (void*)&strResponse);
//ssl 如果是ssl 设置ssl 不 验证证书,如果要带的话得 把证书加进去 ,这里设置不带
curl_easy_setopt(pCURL, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_easy_setopt(pCURL, CURLOPT_SSL_VERIFYHOST, FALSE);
res = curl_easy_perform(pCURL);
curl_easy_cleanup(pCURL);
return res;
}
CURLcode TForm1::HttpPost(const std::string & strUrl, std::string szJson,std::string & strResponse,int nTimeout){
CURLcode res;
char szJsonData[1024];
memset(szJsonData, 0, sizeof(szJsonData));
strcpy(szJsonData, szJson.c_str());
CURL* pCURL = curl_easy_init();
struct curl_slist* headers = NULL;
if (pCURL == NULL) {
return CURLE_FAILED_INIT;
}
CURLcode ret;
ret = curl_easy_setopt(pCURL, CURLOPT_URL, strUrl.c_str());
ret = curl_easy_setopt(pCURL, CURLOPT_POST, 1L);
headers = curl_slist_append(headers,"content-type:application/json");
ret = curl_easy_setopt(pCURL, CURLOPT_HTTPHEADER, headers);
ret = curl_easy_setopt(pCURL, CURLOPT_POSTFIELDS, szJsonData);
ret = curl_easy_setopt(pCURL, CURLOPT_TIMEOUT, nTimeout);
//ssl 如果是ssl 设置ssl 不 验证证书,如果要带的话得 把证书加进去 ,这里设置不带
ret =curl_easy_setopt(pCURL, CURLOPT_SSL_VERIFYPEER, FALSE);
ret =curl_easy_setopt(pCURL, CURLOPT_SSL_VERIFYHOST, FALSE);
ret = curl_easy_setopt(pCURL, CURLOPT_WRITEFUNCTION, receive_data);
ret = curl_easy_setopt(pCURL, CURLOPT_WRITEDATA, (void*)&strResponse);
res = curl_easy_perform(pCURL);
curl_easy_cleanup(pCURL);
return res;
};
demo
// get 请求
string strURL = "http://www.aidigong.com/";
string strResponse;
CURLcode nRes = HttpGet(strURL, strResponse,300);
size_t nSrcLength = strResponse.length();
Memo1->Lines->Text = Utf8ToAnsi((strResponse.data()));