C++ libcurl库使用
常用方法
- 调用curl_global_init()初始化libcurl
- 调用curl_easy_init()函数得到easy_interface型指针
- 调用curl_easy_setopt()设置传输选项
- 调用curl_easy_perform()函数完成传输任务
- 调用curl_easy_cleanup()释放内存
- 调用curl_global_cleanup释放libcurl
函数说明
-
CURLcode curl_global_init(long flags);
描述:
这个函数只能用一次,如果这个函数在curl_easy_init()函数调用时还没调用,它会由libcurl库自动调用。
参数:flags
CURL_GLOBAL_ALL //初始化所有的可能的调用
CURL_GLOBAL_SSL //初始化支持安全套接字层
CURL_GLOBAL_WIN32 //初始化win32套接字库
CURL_GLOBAL_NOTHING //没有额外的初始化
-
void curl_global_cleanup(void);
描述:在结束libcurl使用的时候,用来对curl_global_init做的工作清理。
-
char* curl_version()
描述:返回当前libcurll库的版本
-
CURL* curl_easy_init();
描述:用来初始化一个CURL指针,相应的在调用结束时要用curl_easy_cleanup函数清理
-
void curl_easy_cleanup(CURL* handle);
描述:这个调用用来结束一个会话,与curl_easy_init配合使用。
-
CURLcode curl_easy_setopt(CURL* handle, CURLoption option, parameter);
描述:这个函数最重要了,它告诉curl库,程序要有什么行为。
参数:
- CURL类型的指针
- 各种CURLoption类型的选项。在curl.h库中定义。
- parameter这个参数根据第二参数的类型,传递合适的参数。
-
CURLcode curl_easy_perform(CURL* handle);
描述:这个函数在初始化CURL类型的指针,以及curl_easy_setop完成后调用。
-
void curl_global_cleanup(void);
描述:释放libcurl
curl_easy_setopt介绍
CURLOPT_URL :设置访问URL
CURLOPT_WRITEFUNCTION,CURLOPT_WRITEDATA:回调函数圆形为:size_t function(void* ptr, size_t size, size_t nmemb, void* stream);函数将在libcurl接收到数据后被调用,因此该函数可以用来保存数据,处理下载文件等。CURLOPT_WRITEDATA用于设置CURLOPT_WRITEFUNCTION函数中的stream指针的类型。
CURLOPT_HEADERFUNCTION,CURLOPT_HEADERDATA:回调圆形为: size_t function(void* ptr, size_t size, size_t nmemb, void* stream);libcurl接收到http头部数据后将调用该函数。CURLOPT_HEADERDATA设置传递给CURLOPT_HEADERFUNCTION函数的stream指针类型。
示例一:
//回调函数
size_t write_data(char* buffer, size_t size, size_t nmemb, void* userp)
{
string data(buffer, size * nmemb);//数据
cout << data << endl;
return size * nmemb;
}
int main()
{
stringstream out;
curl_global_init(CURL_GLOBAL_WIN32);
string url = "http://www.baidu.com";
url = "http://127.0.0.1:1205/remote/";
CURL* cu = curl_easy_init();
curl_easy_setopt(cu, CURLOPT_URL, url.c_str());//设置访问地址
curl_easy_setopt(cu, CURLOPT_WRITEFUNCTION, write_data);//设置数据回调函数
curl_easy_setopt(cu, CURLOPT_WRITEDATA, &out);//设置回调函数形参
curl_easy_perform(cu);//请求调用,阻塞
curl_easy_cleanup(cu);//清理curl*
curl_global_cleanup();//清理libcurl
getchar();
}
POST数据,增加设置
curl_easy_setopt(cu, CURLOPT_POST, 1);//设置为post
char data[] = { 'a', 'b', 'c', 'd', 0, 'e', 'f' };
curl_easy_setopt(cu, CURLOPT_POSTFIELDS, data);//post数据
curl_easy_setopt(cu, CURLOPT_POSTFIELDSIZE, sizeof(data)); //post数据长度
下载http文件:
//回调函数
size_t write_data(void* buffer, size_t size, size_t nmemb, void* userp)
{
fstream* fs = static_cast<fstream*>(userp);
if (fs != nullptr)
{
fs->write((char*)buffer, size * nmemb);
}
//cout << size * nmemb << endl;
return size * nmemb;
}
//进度函数
int progress_func(char* progress_data, double t, double d, double ultotal, double ulnow)
{
printf("%s %g / %g (%g %%)
", progress_data, d, t, d*100.0 / t);
return 0;
}
int main()
{
stringstream out;
string url = "http://192.168.51.224:8080/jenkins/job/RG-UClass-Client3.0/lastSuccessfulBuild/artifact/PackUp/Output/3.00.0_Build_2021030512.exe";
CURL* cu = curl_easy_init();
curl_easy_setopt(cu, CURLOPT_URL, url.c_str());//设置访问地址
fstream fs("wo.exe", ios::binary | ios::out);
curl_easy_setopt(cu, CURLOPT_WRITEFUNCTION, write_data);//设置数据回调函数
curl_easy_setopt(cu, CURLOPT_WRITEDATA, &fs);//设置回调函数形参
curl_easy_setopt(cu, CURLOPT_NOPROGRESS, FALSE);//回调进度函数,必须设置为FALSE
char progress_data[] = "*";
curl_easy_setopt(cu, CURLOPT_PROGRESSFUNCTION, progress_func);//回调进度函数
curl_easy_setopt(cu, CURLOPT_PROGRESSDATA, progress_data);
curl_easy_perform(cu);//请求调用,阻塞
curl_easy_cleanup(cu);//清理curl*
curl_global_cleanup();//清理libcurl
fs.close();
getchar();
}