当前位置:网站首页>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
边栏推荐
- 中国氮化硅陶瓷基板行业研究与投资前景报告(2022版)
- Encryption and decryption of tinyurl in leetcode
- Research Report on development monitoring and investment prospects of China's smart environmental protection industry (2022 Edition)
- Transition technology from IPv4 to IPv6
- 走进微信小程序
- 单例模式的懒汉模式跟恶汉模式的区别
- 字节跳动数据平台技术揭秘:基于 ClickHouse 的复杂查询实现与优化
- [Verilog quick start of Niuke network question brushing series] ~ priority encoder circuit ①
- 【splishsplash】关于如何在GUI和json上接收/显示用户参数、MVC模式和GenParam
- In depth evaluation and development trend prediction report of China's ice cream market (2022 Edition)
猜你喜欢

Vulnhub range hacksudo Thor

官宣!香港科技大学(广州)获批!

多线程使用不当导致的 OOM

换掉UUID,NanoID更快更安全!

SQL question brushing 586 Customers with the most orders

Alibaba cloud, Zhuoyi technology beach grabbing dialogue AI

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"

C language input / output stream and file operation
![[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
随机推荐
LeetCode中等题之TinyURL 的加密与解密
【PyG】文档总结以及项目经验(持续更新
深度优先遍历和广度优先遍历[通俗易懂]
中国锦纶长丝缝纫线发展预测与投资方向研究报告(2022版)
反射型XSS漏洞
vulnhub靶场-hacksudo - Thor
Research and investment strategy report of neutral protease industry in China (2022 Edition)
How to use etcd to realize distributed /etc directory
智能运维实战:银行业务流程及单笔交易追踪
Vulnhub range hacksudo Thor
Gold, silver and four want to change jobs, so we should seize the time to make up
Object. fromEntries()
Concatenate strings to get the result with the smallest dictionary order
C語言輸入/輸出流和文件操作
Maizeer: the two batches of products reported by the media have been taken off the shelves and sealed, and consumer appeals are accepted
Determine whether the linked list is a palindrome linked list
Yyds dry inventory MySQL RC transaction isolation level implementation
Openlayers 自定义气泡框以及定位到气泡框
Redis6.0 新功能
The amazing open source animation library is not only awesome, but also small