学习网站
官网
函数及其作用
- 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;
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");
char *content = new char[filestat.st_size];
fread(content, 1, filestat.st_size, file);
fclose(file);
curl = curl_easy_init();
if (curl)
{
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");
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
std::string url = "http://192.168.88.128:8888" + path;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
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) {
std::cout << "curl_easy_perform() failed:" << curl_easy_strerror(res);
}
std::cout<<"main data:"<<data<<std::endl;
curl_slist_free_all(headers);
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;
struct curl_slist* headers = NULL;
curl = curl_easy_init();
if (curl)
{
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");
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
std::string url = "http://192.168.88.128:8888" + path;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
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) {
std::cout << "curl_easy_perform() failed:" << curl_easy_strerror(res);
}
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
fclose(fp);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!