当前位置:网站首页>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 input / output stream and file operation
- China nylon 11 industry research and future forecast report (2022 Edition)
- JDBC:深入理解PreparedStatement和Statement[通俗易懂]
- 单例模式的懒汉模式跟恶汉模式的区别
- 【C語言補充】判斷明天是哪一天(明天的日期)
- 【splishsplash】关于如何在GUI和json上接收/显示用户参数、MVC模式和GenParam
- (17) DAC conversion experiment
- Research and investment strategy report of neutral protease industry in China (2022 Edition)
- C language implementation of sum of two numbers [easy to understand]
- 走进微信小程序
猜你喜欢
String类
Leetcode records - sort -215, 347, 451, 75
如何使用 etcd 实现分布式 /etc 目录
Mysql database - Advanced SQL statement (2)
Redis Distributed Lock
Roewe rx5's "a little more" product strategy
ShenYu 网关开发:在本地启用运行
Redis 分布式鎖
ACM MM 2022视频理解挑战赛视频分类赛道冠军AutoX团队技术分享
Free lottery | explore the future series of blind box digital copyright works of "abadou" will be launched on the whole network!
随机推荐
中国PBAT树脂市场预测及战略研究报告(2022版)
(1) CNN network structure
Chinese diosgenin market forecast and investment strategy report (2022 Edition)
JDBC:深入理解PreparedStatement和Statement[通俗易懂]
Judge whether a binary tree is a balanced binary tree
[C language supplement] judge which day tomorrow is (tomorrow's date)
深度优先遍历和广度优先遍历[通俗易懂]
【牛客网刷题系列 之 Verilog快速入门】~ 优先编码器电路①
Openlayers 自定义气泡框以及定位到气泡框
中国酶制剂市场预测与投资战略研究报告(2022版)
GameFramework食用指南
越来越多地使用 SLO 来实现可观测性|DevOps
为什么你要考虑使用Prisma
Machine learning 11 clustering, outlier discrimination
SQL question brushing 1050 Actors and directors who have worked together at least three times
ACL 2022 | 分解的元学习小样本命名实体识别
中国锦纶长丝缝纫线发展预测与投资方向研究报告(2022版)
Introduction to software engineering - Chapter 6 - detailed design
Transition technology from IPv4 to IPv6
Shenyu gateway development: enable and run locally