当前位置:网站首页>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
边栏推荐
- Redis distributed lock
- Chinese diosgenin market forecast and investment strategy report (2022 Edition)
- 如何使用 etcd 实现分布式 /etc 目录
- How to use etcd to realize distributed /etc directory
- Research Report on development monitoring and investment prospects of China's smart environmental protection industry (2022 Edition)
- 如何写出好代码 — 防御式编程指南
- China nylon 11 industry research and future forecast report (2022 Edition)
- Girls who want to do software testing look here
- 为什么你要考虑使用Prisma
- DNS
猜你喜欢

Why should you consider using prism

Vulnhub range hacker_ Kid-v1.0.1

(28) Shape matching based on contour features

The amazing open source animation library is not only awesome, but also small

【C补充】【字符串】按日期排序显示一个月的日程
![Integer array merge [JS]](/img/0d/70535e0eb1c299bda25159b58c70d7.png)
Integer array merge [JS]

Jojogan practice

Free lottery | explore the future series of blind box digital copyright works of "abadou" will be launched on the whole network!

(1) CNN network structure

String class
随机推荐
vulnhub靶场-Hacker_Kid-v1.0.1
Encryption and decryption of tinyurl in leetcode
在MeterSphere接口测试中如何使用JMeter函数和MockJS函数
剑指 Offer 20. 表示数值的字符串
中国超高分子量聚乙烯产业调研与投资前景报告(2022版)
整形数组合并【JS】
Sword finger offer II 015 All modifiers in the string
官宣!香港科技大学(广州)获批!
多线程并发之CountDownLatch阻塞等待
MySQL learning summary
[Verilog quick start of Niuke network question brushing series] ~ priority encoder circuit ①
Please, stop painting star! This has nothing to do with patriotism!
libcurl下载文件的代码示例
中国酶制剂市场预测与投资战略研究报告(2022版)
Pytest learning notes (13) -allure of allure Description () and @allure title()
阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
FRP intranet penetration, reverse proxy
Openlayers customize bubble boxes and navigate to bubble boxes
Transition technology from IPv4 to IPv6
Research and investment strategy report of hydroxypropyl beta cyclodextrin industry in China (2022 Edition)