当前位置:网站首页>libcurl下载文件的代码示例
libcurl下载文件的代码示例
2022-07-01 16:41:00 【russle】
C++中使用libcurl下载文件示例
备注:简单示例,仅供参考,个人c++
代码
//
// 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>
//判断url是否存在
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;
}
执行如下命令
g++ curl_file.cpp -std=c++11 -lcurl -o b.out;./b.out
结果如下
statusCode:200
file writen byte:7801
file writen byte:4096
file writen byte:3906
file writen byte:8304
done
文件写入到了index.html
边栏推荐
- Stegano in the world of attack and defense
- 判断二叉树是否为二叉搜索树
- Bugku's file contains
- 独家消息:阿里云悄然推出RPA云电脑,已与多家RPA厂商开放合作
- 重磅披露!上百个重要信息系统被入侵,主机成为重点攻击目标
- How to solve the keyboard key failure of notebook computer
- Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
- Go language source level debugger delve
- Flux d'entrées / sorties et opérations de fichiers en langage C
- 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"
猜你喜欢
Tutorial on the principle and application of database system (003) -- MySQL installation and configuration: manually configure MySQL (Windows Environment)
Tutorial on principles and applications of database system (004) -- MySQL installation and configuration: resetting MySQL login password (Windows Environment)
Basic use of MySQL
Soft test network engineer full truth simulation question (including answer and analysis)
How to solve the problem that the battery icon of notebook computer does not display
【PyG】文档总结以及项目经验(持续更新
【C语言基础】12 字符串
Mlperf training v2.0 list released, with the same GPU configuration, the performance of Baidu PaddlePaddle ranks first in the world
Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
Today, at 14:00, 15 ICLR speakers from Hong Kong University, Beihang, Yale, Tsinghua University, Canada, etc. continue!
随机推荐
How to cancel automatic search and install device drivers for laptops
Sword finger offer II 015 All modifiers in the string
How wild are hackers' ways of making money? CTF reverse entry Guide
AI college entrance examination volunteer filling: the gods of Dachang fight, and candidates pay to watch
Judge whether the binary tree is a binary search tree
China carbon disulfide industry research and investment strategy report (2022 Edition)
数据库系统原理与应用教程(005)—— yum 离线安装 MySQL5.7(Linux 环境)
Pytest learning notes (13) -allure of allure Description () and @allure title()
你还在用收费的文档管理工具?我这有更牛逼的选择!完全免费
【splishsplash】关于如何在GUI和json上接收/显示用户参数、MVC模式和GenParam
How to maintain the laptop battery
判断二叉树是否为二叉搜索树
Transition technology from IPv4 to IPv6
Rhcsa Road
Origin2018 installation and use (sorting)
How to solve the problem that the battery icon of notebook computer does not display
Is the securities account given by the head teacher of goucai school safe? Can I open an account?
Are you still using charged document management tools? I have a better choice! Completely free
sql刷题586. 订单最多的客户
unity3d扩展工具栏