当前位置:网站首页>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
边栏推荐
- String class
- 机器学习11-聚类,孤立点判别
- 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"
- 提交review时ReviewBoard出现500错误解决方法
- Redis6.0 new features
- 中国PBAT树脂市场预测及战略研究报告(2022版)
- 深度优先遍历和广度优先遍历[通俗易懂]
- China BMS battery management system Market Research Report (2022 Edition)
- Openlayers 自定义气泡框以及定位到气泡框
- (28) Shape matching based on contour features
猜你喜欢

Machine learning 11 clustering, outlier discrimination

Redis distributed lock

Official announcement! Hong Kong University of science and Technology (Guangzhou) approved!

SystemVerilog-结构体(二)

Vulnhub range hacker_ Kid-v1.0.1
荣威 RX5 的「多一点」产品策略

(27) Open operation, close operation, morphological gradient, top hat, black hat

Redis6.0 新功能

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"

Redis6.0 new features
随机推荐
单例模式的懒汉模式跟恶汉模式的区别
走进微信小程序
China carbon disulfide industry research and investment strategy report (2022 Edition)
整形数组合并【JS】
拼接字符串,得到字典序最小的结果
想做软件测试的女孩子看这里
提交review时ReviewBoard出现500错误解决方法
深度优先遍历和广度优先遍历[通俗易懂]
Openlayers 自定义气泡框以及定位到气泡框
英特尔开源深度学习工具库 OpenVINO,将加大与本土软硬件方合作,持续开放
Encryption and decryption of tinyurl in leetcode
(十六)ADC转换实验
Research Report on China's enzyme Market Forecast and investment strategy (2022 Edition)
GameFramework食用指南
China metallocene polyethylene (MPE) Industry Research Report (2022 Edition)
中国酶制剂市场预测与投资战略研究报告(2022版)
重磅披露!上百个重要信息系统被入侵,主机成为重点攻击目标
Redis 分布式锁
C language implementation of sum of two numbers [easy to understand]
[pyg] document summary and project experience (continuously updated