学习网站
函数及其作用
- CURL *curl_easy_init(); //初始化一个curl,如果成功返回一个CURL句柄
- void curl_easy_cleanup(CURL *handle); //用于结束一个curl
- CURLcode curl_easy_setopt(CURL *handle, CURLoption option, parameter); //设置一个curl上的选项
- CURLcode curl_easy_perform(CURL *easy_handle); //阻塞的执行文件传输
- CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ... ); //从curl中获取信息
注意
- CURLcode curl_global_init(long flags); //libcurl其他所有函数被调用之前这函数至少需要被调用一次,此函数非线程安全,只需要调用一次,最好在主线程中调用
示例代码
curl使用post上传文件
size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) //回调函数
{
std::string *str = (std::string*)stream;
(*str).append((char*)ptr, realSize);
return size * nmemb;
}
std::string data;
CURL* curl = NULL;
CURLcode res = CURLE_OK;
//HTTP报文头
struct curl_slist* headers = NULL;
struct stat filestat;
if (stat(path.c_str(), &filestat) < 0) {
exit(-1);
}
if (!(filestat.st_mode & S_IROTH)) {
exit(-1);
}
if (S_ISDIR(filestat.st_mode)) { //判断用户对该文件所拥有的权限
exit(-1);
}
FILE *file = fopen(path.c_str(), "rb"); //path为文件路径
char *content = new char[filestat.st_size];
fread(content, 1, filestat.st_size, file);
fclose(file);
//初始化easy handler句柄
curl = curl_easy_init();
if (curl)
{
//构建HTTP报文头
curl_slist *http_headers = NULL;
std::string cookie = "Set-Cookie: " + encodePassword;
http_headers = curl_slist_append(http_headers, cookie.c_str()); //设置请求cookie
http_headers = curl_slist_append(http_headers, "charsets: utf-8");
//设置method为post
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
//设置post请求的url地址
std::string url = "http://192.168.88.128:8888" + path;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
//设置HTTP头
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, http_headers);
//设置发送超时时间
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
//将文件内容设为请求体
curl_easy_setopt(curl,CURLOPT_POSTFIELDS,content);
curl_easy_setopt(curl,CURLOPT_POSTFIELDSIZE,filestat.st_size); //因为是以二进制方式发送数据所以需要指定数据的长度
//设置应答报文体内容解析回调函数
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&data);
//执行单条请求
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
//curl_easy_strerror进行出错打印
std::cout << "curl_easy_perform() failed:" << curl_easy_strerror(res);
}
std::cout<<"main data:"<<data<<std::endl;
curl_slist_free_all(headers);
//这个调用用来结束一个会话.与curl_easy_init配合着用
curl_easy_cleanup(curl);
}
curl使用GET并将返回的数据存入文件
size_t receive_file(void *buffer, size_t size, size_t nmemb, FILE *file) { //回调函数
size_t r_size = fwrite(buffer, size, nmemb, file);
return r_size;
}
std::string data;
CURL* curl = NULL;
CURLcode res = CURLE_OK;
//HTTP报文头
struct curl_slist* headers = NULL;
//初始化easy handler句柄
curl = curl_easy_init();
if (curl)
{
//构建HTTP报文头
curl_slist *http_headers = NULL;
std::string cookie = "Set-Cookie: " + encodePassword;
http_headers = curl_slist_append(http_headers, cookie.c_str());
http_headers = curl_slist_append(http_headers, "charsets: utf-8");
//设置method为GET
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
//设置post请求的url地址
std::string url = "http://192.168.88.128:8888" + path;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
//设置HTTP头
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, http_headers);
//设置发送超时时间
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
//打开文件
std::string route = "test.txt";
FILE* fp;
if ((fp = fopen(route.c_str(), "wb+")) == NULL)
{
printf("File.\n");
}
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, receive_file);//设置回调函数
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); //将文件描述符传递给回调函数
//执行单条请求
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
//curl_easy_strerror进行出错打印
std::cout << "curl_easy_perform() failed:" << curl_easy_strerror(res);
}
curl_slist_free_all(headers);
//这个调用用来结束一个会话.与curl_easy_init配合着用
curl_easy_cleanup(curl);
fclose(fp);
}