当前位置:网站首页>Code example of libcurl download file
Code example of libcurl download file
2022-07-01 17:18:00 【russle】
C++ Use in libcurl Download sample files
remarks : A simple example , For reference only , personal c++
Code
//
// 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>
// Judge url Whether there is
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;
}
Execute the following command
g++ curl_file.cpp -std=c++11 -lcurl -o b.out;./b.out
give the result as follows
statusCode:200
file writen byte:7801
file writen byte:4096
file writen byte:3906
file writen byte:8304
done
The file is written to index.html
边栏推荐
- C language implementation of sum of two numbers [easy to understand]
- String class
- 阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
- Vulnhub range hacksudo Thor
- 中国一次性卫生用品生产设备行业深度调研报告(2022版)
- 深度优先遍历和广度优先遍历[通俗易懂]
- 中国酶制剂市场预测与投资战略研究报告(2022版)
- 字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
- LeetCode中等题之TinyURL 的加密与解密
- Shenyu gateway development: enable and run locally
猜你喜欢

SQL question brushing 584 Looking for user references

SQL注入漏洞(Mysql与MSSQL特性)

Iommu/smmuv3 code analysis (10) page table operation
荣威 RX5 的「多一点」产品策略

英特尔开源深度学习工具库 OpenVINO,将加大与本土软硬件方合作,持续开放

【牛客网刷题系列 之 Verilog快速入门】~ 优先编码器电路①

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

Babbitt | yuan universe daily must read: Naixue coin, Yuan universe paradise, virtual stock game Do you understand Naixue's tea's marketing campaign of "operation pull full"

Transition technology from IPv4 to IPv6

Oom caused by improper use of multithreading
随机推荐
(十六)ADC转换实验
C language implementation of sum of two numbers [easy to understand]
Mysql database - Advanced SQL statement (2)
China biodegradable plastics market forecast and investment strategy report (2022 Edition)
【C补充】【字符串】按日期排序显示一个月的日程
求求你们,别再刷 Star 了!这跟“爱国”没关系!
String类
ACL 2022 | 分解的元学习小样本命名实体识别
阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
反射型XSS漏洞
FRP intranet penetration, reverse proxy
RadHat搭建内网YUM源服务器
China benzene hydrogenation Market Research and investment forecast report (2022 Edition)
Research Report on development monitoring and investment prospects of China's smart environmental protection industry (2022 Edition)
中国氮化硅陶瓷基板行业研究与投资前景报告(2022版)
两数之和c语言实现[通俗易懂]
Roewe rx5's "a little more" product strategy
PHP实现敏感词过滤系统「建议收藏」
Concatenate strings to get the result with the smallest dictionary order
(27) Open operation, close operation, morphological gradient, top hat, black hat