当前位置:网站首页>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
边栏推荐
- [Supplément linguistique c] déterminer quel jour est demain (date de demain)
- (17) DAC conversion experiment
- Research Report on China's enzyme Market Forecast and investment strategy (2022 Edition)
- 求求你们,别再刷 Star 了!这跟“爱国”没关系!
- Redis distributed lock
- C language implementation of sum of two numbers [easy to understand]
- 整形数组合并【JS】
- 如何使用 etcd 实现分布式 /etc 目录
- 重磅披露!上百个重要信息系统被入侵,主机成为重点攻击目标
- 剑指 Offer II 105. 岛屿的最大面积
猜你喜欢

【splishsplash】关于如何在GUI和json上接收/显示用户参数、MVC模式和GenParam

Redis distributed lock

多线程使用不当导致的 OOM

There is a new breakthrough in quantum field: the duration of quantum state can exceed 5 seconds

换掉UUID,NanoID更快更安全!

重磅披露!上百个重要信息系统被入侵,主机成为重点攻击目标

如何使用 etcd 实现分布式 /etc 目录
![[pyg] document summary and project experience (continuously updated](/img/b4/75da8c3e657069be4e3e3bfd5b2dc0.png)
[pyg] document summary and project experience (continuously updated

走进微信小程序

Leetcode records - sort -215, 347, 451, 75
随机推荐
字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
如何使用 etcd 实现分布式 /etc 目录
如何写出好代码 — 防御式编程指南
【splishsplash】关于如何在GUI和json上接收/显示用户参数、MVC模式和GenParam
Research and investment strategy report of hydroxypropyl beta cyclodextrin industry in China (2022 Edition)
Alibaba cloud, Zhuoyi technology beach grabbing dialogue AI
SQL question brushing 1050 Actors and directors who have worked together at least three times
JDBC:深入理解PreparedStatement和Statement[通俗易懂]
(1) CNN network structure
ACM MM 2022视频理解挑战赛视频分类赛道冠军AutoX团队技术分享
DNS
Redis 分布式鎖
Official announcement! Hong Kong University of science and Technology (Guangzhou) approved!
Cookies and session keeping technology
多线程使用不当导致的 OOM
Basic usage of Frida
unity3d扩展工具栏
Iommu/smmuv3 code analysis (10) page table operation
6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
(十七)DAC转换实验