当前位置:网站首页>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
边栏推荐
- How to cancel automatic search and install device drivers for laptops
- 剑指 Offer II 105. 岛屿的最大面积
- Rhcsa Road
- Internet News: "20220222" get together to get licenses; Many products of Jimi have been affirmed by consumers; Starbucks was fined for using expired ingredients in two stores
- 剑指 Offer II 015. 字符串中的所有变位词
- 6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
- [flask introduction series] cookies and session
- Rhcsa Road
- 可迭代对象与迭代器、生成器的区别与联系
- Concatenate strings to get the result with the smallest dictionary order
猜你喜欢
模板引擎Velocity 基础
Building blocks for domestic databases, stonedb integrated real-time HTAP database is officially open source!
今天14:00 | 港大、北航、耶鲁、清华、加大等15位ICLR一作讲者精彩继续!
Buuctf gold III
[live broadcast appointment] database obcp certification comprehensive upgrade open class
如何写出好代码 — 防御式编程指南
SQL question brushing 1050 Actors and directors who have worked together at least three times
sql刷题627. 变更性别
Girls who want to do software testing look here
Detailed explanation of activity life cycle and startup mode
随机推荐
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
Tutorial on principles and applications of database system (004) -- MySQL installation and configuration: resetting MySQL login password (Windows Environment)
可迭代对象与迭代器、生成器的区别与联系
SystemVerilog-结构体(二)
【flask入门系列】Cookie与Session
P2893 [USACO08FEB] Making the Grade G(dp&优先队列)
Endeavouros mobile hard disk installation
China carbon disulfide industry research and investment strategy report (2022 Edition)
Research and investment strategy report of China's sodium sulfate industry (2022 Edition)
Chinese diosgenin market forecast and investment strategy report (2022 Edition)
Machine learning 11 clustering, outlier discrimination
China benzene hydrogenation Market Research and investment forecast report (2022 Edition)
Borui data integrated intelligent observable platform was selected into the "Yunyuan production catalogue" of China Academy of communications in 2022
【PyG】文档总结以及项目经验(持续更新
【C語言補充】判斷明天是哪一天(明天的日期)
Redis 分布式鎖
求求你们,别再刷 Star 了!这跟“爱国”没关系!
Kali install Nessus
Is the securities account given by the head teacher of goucai school safe? Can I open an account?
sql刷题627. 变更性别