当前位置:网站首页>libcurl下载文件的代码示例
libcurl下载文件的代码示例
2022-07-01 16:41:00 【russle】
C++中使用libcurl下载文件示例
备注:简单示例,仅供参考,个人c++
代码
//
// sudo apt-get remove libcurl4
// sudo apt-get install libcurl4-openssl-dev
//
// g++ curl_file.cpp -std=c++11 -lcurl -o a.out;./a.out
#include <iostream>
#include <stdlib.h>
#include <curl/curl.h>
//判断url是否存在
int checkUrlIsExist(const char * URL) {
CURL * curl_fd = curl_easy_init();
CURLcode code;
CURLINFO response_code = CURLINFO_NONE;
curl_easy_setopt(curl_fd, CURLOPT_NOBODY, 1);
curl_easy_setopt(curl_fd, CURLOPT_TIMEOUT, 3);
curl_easy_setopt(curl_fd, CURLOPT_URL, URL);
code = curl_easy_perform(curl_fd);
if (code == CURLE_OK) {
curl_easy_getinfo(curl_fd, CURLINFO_RESPONSE_CODE, &response_code);
}
curl_easy_cleanup(curl_fd);
return response_code;
}
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
// prints as unsigned decimal
printf("file writen byte:%zu\n", written);
return written;
}
int main()
{
std::string url = "https://www.jianshu.com";
const char *cUrl = url.c_str();
int statusCode = checkUrlIsExist(cUrl);
if (statusCode == 404) {
printf("url does not exist\n");
} else {
printf("statusCode:%d\n", statusCode);
}
CURL *curl;
FILE *fp;
CURLcode res;
char outfilename[FILENAME_MAX] = "./index.html";
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, cUrl);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
fclose(fp);
}
printf("done\n");
return 0;
}
执行如下命令
g++ curl_file.cpp -std=c++11 -lcurl -o b.out;./b.out
结果如下
statusCode:200
file writen byte:7801
file writen byte:4096
file writen byte:3906
file writen byte:8304
done
文件写入到了index.html
边栏推荐
- Rhcsa Road
- Leetcode 216 combined summation III -- backtracking method
- SQL question brushing 627 Change gender
- Is the securities account given by the head teacher of goucai school safe? Can I open an account?
- 【flask入门系列】Cookie与Session
- P2893 [USACO08FEB] Making the Grade G(dp&优先队列)
- How to optimize repeated if err in go language= Nil template code?
- 英特尔开源深度学习工具库 OpenVINO,将加大与本土软硬件方合作,持续开放
- Soft test software designer full truth simulation question (including answer analysis)
- Girls who want to do software testing look here
猜你喜欢

博睿数据一体化智能可观测平台入选中国信通院2022年“云原生产品名录”

String类

巴比特 | 元宇宙每日必读:奈雪币、元宇宙乐园、虚拟股票游戏...奈雪的茶这波“操作拉满”的营销活动你看懂了吗?...

Hi Fun Summer, play SQL planner with starrocks!

Tutorial on the principle and application of database system (003) -- MySQL installation and configuration: manually configure MySQL (Windows Environment)

Free lottery | explore the future series of blind box digital copyright works of "abadou" will be launched on the whole network!

Soft test network engineer full truth simulation question (including answer and analysis)

你还在用收费的文档管理工具?我这有更牛逼的选择!完全免费

免费抽奖 | 《阿巴豆》探索未来系列盲盒数字版权作品全网首发!

C语言输入/输出流和文件操作
随机推荐
[nodemon] app crashed - waiting for file changes before starting... resolvent
Rhcsa Road
Installation and use of sqoop
模板引擎Velocity 基礎
判断链表是否是回文链表
Machine learning 11 clustering, outlier discrimination
How to restore the system of Sony laptop
VMware virtual machine failed during startup: VMware Workstation is incompatible with hyper-v
为国产数据库添砖加瓦,StoneDB 一体化实时 HTAP 数据库正式开源!
FPN network details
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
Ring iron pronunciation, dynamic and noiseless, strong and brilliant, magic wave hifiair Bluetooth headset evaluation
China BMS battery management system Market Research Report (2022 Edition)
Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world
mysql -- explain性能优化
Rhcsa Road
MySQL learning summary
独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作
Alibaba cloud, Zhuoyi technology beach grabbing dialogue AI
[JetsonNano] [教程] [入门系列] [三] 搭建TensorFlow环境