当前位置:网站首页>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语言基础】12 字符串
- The amazing open source animation library is not only awesome, but also small
- AI college entrance examination volunteer filling: the gods of Dachang fight, and candidates pay to watch
- String的trim()和substring()详解
- 多线程并发之CountDownLatch阻塞等待
- 如何写出好代码 — 防御式编程指南
- 【Try to Hack】vulnhub DC4
- [wrung Ba wrung Ba is 20] [essay] why should I learn this in college?
- Yyds dry inventory MySQL RC transaction isolation level implementation
- DNS
猜你喜欢
vulnhub靶场-Hacker_Kid-v1.0.1
Vulnhub range hacksudo Thor
Flux d'entrées / sorties et opérations de fichiers en langage C
PETRv2:一个多摄像头图像3D感知的统一框架
Roewe rx5's "a little more" product strategy
Redis 分布式锁
[flask introduction series] cookies and session
6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
Cookies and session keeping technology
整形数组合并【JS】
随机推荐
C language implementation of sum of two numbers [easy to understand]
[pyg] document summary and project experience (continuously updated
Research and investment strategy report of hydroxypropyl beta cyclodextrin industry in China (2022 Edition)
GameFramework食用指南
ACL 2022 | 分解的元学习小样本命名实体识别
机器学习11-聚类,孤立点判别
如何写出好代码 — 防御式编程指南
[Supplément linguistique c] déterminer quel jour est demain (date de demain)
剑指 Offer II 105. 岛屿的最大面积
libcurl下载文件的代码示例
Redis 分布式锁
Report on Market Research and investment prospects of ammonium dihydrogen phosphate industry in China (2022 Edition)
Free lottery | explore the future series of blind box digital copyright works of "abadou" will be launched on the whole network!
pyqt5中,在控件上画柱状图
(十七)DAC转换实验
China benzene hydrogenation Market Research and investment forecast report (2022 Edition)
Computed property “xxx“ was assigned to but it has no setter.
走进微信小程序
Report on research and investment prospects of UHMWPE industry in China (2022 Edition)
判断链表是否是回文链表