当前位置:网站首页>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
边栏推荐
- Tutorial on the principle and application of database system (003) -- MySQL installation and configuration: manually configure MySQL (Windows Environment)
- China BMS battery management system Market Research Report (2022 Edition)
- sql刷题627. 变更性别
- Soft test software designer full truth simulation question (including answer analysis)
- Pytest learning notes (13) -allure of allure Description () and @allure title()
- [live broadcast appointment] database obcp certification comprehensive upgrade open class
- Concatenate strings to get the result with the smallest dictionary order
- How to use etcd to realize distributed /etc directory
- Tutorial on principles and applications of database system (004) -- MySQL installation and configuration: resetting MySQL login password (Windows Environment)
- Sword finger offer II 015 All modifiers in the string
猜你喜欢

嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!

Basic use of MySQL

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

Activity的生命周期和启动模式详解

6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单

Tutorial on the principle and application of database system (003) -- MySQL installation and configuration: manually configure MySQL (Windows Environment)
![[live broadcast appointment] database obcp certification comprehensive upgrade open class](/img/50/83a533f4e8a60f90e03b991385c08d.jpg)
[live broadcast appointment] database obcp certification comprehensive upgrade open class

Tutorial on the principle and application of database system (002) -- MySQL installation and configuration: MySQL software uninstallation (Windows Environment)
![[JetsonNano] [教程] [入门系列] [三] 搭建TensorFlow环境](/img/0e/52e37527bc717c7a55741725087bad.png)
[JetsonNano] [教程] [入门系列] [三] 搭建TensorFlow环境

Detailed explanation of activity life cycle and startup mode
随机推荐
China benzene hydrogenation Market Research and investment forecast report (2022 Edition)
嗨 FUN 一夏,与 StarRocks 一起玩转 SQL Planner!
String class
How to optimize repeated if err in go language= Nil template code?
Is the securities account given by the head teacher of goucai school safe? Can I open an account?
Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world
Report on Market Research and investment prospects of ammonium dihydrogen phosphate industry in China (2022 Edition)
Tutorial on the principle and application of database system (005) -- Yum offline installation of MySQL 5.7 (Linux Environment)
Défaillance lors du démarrage de la machine virtuelle VMware: le poste de travail VMware n'est pas compatible avec hyper - V...
Tutorial on the principle and application of database system (002) -- MySQL installation and configuration: MySQL software uninstallation (Windows Environment)
SQL question brushing 1050 Actors and directors who have worked together at least three times
巴比特 | 元宇宙每日必读:奈雪币、元宇宙乐园、虚拟股票游戏...奈雪的茶这波“操作拉满”的营销活动你看懂了吗?...
sql刷题627. 变更性别
[jetsonnano] [tutorial] [introductory series] [III] build tensorflow environment
数据库系统原理与应用教程(003)—— MySQL 安装与配置:手工配置 MySQL(windows 环境)
【PyG】文档总结以及项目经验(持续更新
How to cancel automatic search and install device drivers for laptops
想做软件测试的女孩子看这里
Template Engine Velocity Foundation
博睿数据一体化智能可观测平台入选中国信通院2022年“云原生产品名录”