当前位置:网站首页>Simple example of libcurl accessing url saved as file
Simple example of libcurl accessing url saved as file
2022-08-02 02:01:00 【russle】
备注:本文是libcurl easy interfaceThe download file simple example,非prod-ready的代码, 没有提供keep-Alive、超时设置等. 只是一个简单示例.
libcurl是网络工具curl的底层核心.It is very reliable and stable network dependent libraries. 分为easy和multiTwo series of interface. 其中easy是同步调用,相对简单, multi是异步调用, 比较复杂. 本文是easy接口调用示例.
官网链接: https://curl.se/libcurl/
In the website has a lot of sample,Can choose according to need reference code islibcurl的api说明:https://curl.se/libcurl/c/libcurl-tutorial.html
代码
// Copyright (c) 2022 by yqbjtu
//
// sudo apt-get remove libcurl4
// 确保机器已经安装了libcurl库
// sudo apt-get install libcurl4-openssl-dev
// Build command line, Because this article is very simple,A sample file,一条命令搞定
// g++ curl_file.cpp -std=c++11 -lcurl -o libcurdemo.out;
// 执行./libcurdemo.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()
{
CURLcode res;
/* 调用curl_global_init()初始化libcurl */
res = curl_global_init(CURL_GLOBAL_ALL);
if (CURLE_OK != res)
{
std::cout <<"init libcurl failed."<< std::endl;
curl_global_cleanup();
return -1;
}
std::cout << "curl version:" << curl_version() << std::endl;
std::cout << "libcurl download file demo" << std::endl;
std::string url = "https://www.baidu.com";
const char *cUrl = url.c_str();
int statusCode = checkUrlIsExist(cUrl);
if (statusCode == 404) {
std::cout << "page not found, http status 404"<< std::endl;
return -1;
} else {
std::cout << "statusCode:" << statusCode<< std::endl;
}
CURL *curl;
FILE *fp;
CURLcode resultCode;
char outFileName[FILENAME_MAX] = "./index.html";
curl = curl_easy_init();
if (curl) {
fp = fopen(outFileName,"wb");
if (fp == NULL) {
std::cout << "fail to open file " << outFileName << std::endl;
curl_easy_cleanup(curl);
return -1;
}
// Set the header fields、Add request body all throughcurl_easy_setopt完成
curl_easy_setopt(curl, CURLOPT_URL, cUrl);
// 如果没有通过CURLOPT_WRITEFUNCTION属性给easy handle设置回调函数,libcurl会提供一个默认的回调函数,它只是简单的将接收到的数据打印到标准输出.
// 可以通过CURLOPT_WRITEDATA属性给默认回调函数传递一个已经打开的文件指针,用于将数据输出到文件里.
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
/* Switch on full protocol/debug output , Need to look at some intermediate information can open*/
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
resultCode = curl_easy_perform(curl);
std::cout <<"curl resultCode:" << resultCode << std::endl;
if (resultCode != CURLE_OK)
{
std::cout << "fail to access url " << url << std::endl;
return -1;
}
/* always cleanup */
curl_easy_cleanup(curl);
fclose(fp);
}
std::cout << "write data to file " << outFileName << " done" << std::endl;
// 对应开始的curl_global_init
curl_global_cleanup();
return 0;
}
效果
xxx-MacBook-Pro ch5 % g++ curl_file.cpp -std=c++11 -lcurl -o libcurdemo.out;./libcurdemo.out
curl version:libcurl/7.77.0 (SecureTransport) LibreSSL/2.8.3 zlib/1.2.11 nghttp2/1.42.0
libcurl download file demo
statusCode:200
* Trying 110.242.68.3:443...
* Connected to www.baidu.com (110.242.68.3) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/cert.pem
* CApath: none
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: C=CN; ST=beijing; L=beijing; OU=service operation department; O=Beijing Baidu Netcom Science Technology Co., Ltd; CN=baidu.com
* start date: Jul 5 05:16:02 2022 GMT
* expire date: Aug 6 05:16:01 2023 GMT
* subjectAltName: host "www.baidu.com" matched cert's "*.baidu.com"
* issuer: C=BE; O=GlobalSign nv-sa; CN=GlobalSign RSA OV SSL CA 2018
* SSL certificate verify ok.
> GET / HTTP/1.1
Host: www.baidu.com
Accept: */*
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: no-cache
< Connection: keep-alive
< Content-Length: 9508
< Content-Type: text/html
< Date: Sat, 30 Jul 2022 03:34:06 GMT
< P3p: CP=" OTI DSP COR IVA OUR IND COM "
< P3p: CP=" OTI DSP COR IVA OUR IND COM "
< Pragma: no-cache
< Server: BWS/1.1
< Set-Cookie: BAIDUID=BAF6D7E5ACD702EEA47AB9528ACECA23:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
< Set-Cookie: BIDUPSID=BAF6D7E5ACD702EEA47AB9528ACECA23; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
< Set-Cookie: PSTM=1659152046; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
< Set-Cookie: BAIDUID=BAF6D7E5ACD702EED7F6FFB8A23F97DF:FG=1; max-age=31536000; expires=Sun, 30-Jul-23 03:34:06 GMT; domain=.baidu.com; path=/; version=1; comment=bd
< Traceid: 165915204602586155627623073953155317249
< Vary: Accept-Encoding
< X-Frame-Options: sameorigin
< X-Ua-Compatible: IE=Edge,chrome=1
<
file writen byte:192
file writen byte:2358
file writen byte:559
file writen byte:4716
file writen byte:1683
* Connection #0 to host www.baidu.com left intact
curl resultCode:0
write data to file ./index.html done
边栏推荐
- 软件测试功能测试全套常见面试题【开放性思维题】面试总结4-3
- swift项目,sqlcipher3 -&gt; 4,无法打开旧版数据库有办法解决吗
- 乱七八糟的网站
- Huawei's 5-year female test engineer resigns: what a painful realization...
- 【ORB_SLAM2】void Frame::AssignFeaturesToGrid()
- 6-25 Vulnerability Exploitation - irc Backdoor Exploitation
- 力扣(LeetCode)213. 打家劫舍 II(2022.08.01)
- 第一次写对牛客的编程面试题:输入一个字符串,返回该字符串出现最多的字母
- Centos7 安装postgresql并开启远程访问
- 3 Month Tester Readme: 4 Important Skills That Impacted My Career
猜你喜欢

MySQL优化策略

The characteristics and principle of typescript29 - enumeration type

LeetCode刷题日记:74. 搜索二维矩阵

Rust P2P Network Application Combat-1 P2P Network Core Concepts and Ping Program

After graduating from three books, I was rejected by Tencent 14 times, and finally successfully joined Alibaba

typescript30 - any type

Constructor instance method inheritance of typescript37-class (extends)

typescript38-class的构造函数实例方法继承(implement)

LeetCode Brushing Diary: 74. Searching 2D Matrix

Navicat data shows incomplete resolution
随机推荐
"Introduction to Natural Language Processing Practice" Question Answering Robot Based on Knowledge Graph
For effective automated testing, these software testing tools must be collected!!!
Chengdu openGauss user group recruit!
待读书单列表
C language inserted into the characters of simple exercises
LeetCode刷题日记:34、 在排序数组中查找元素的第一个和最后一个位置
Use baidu EasyDL implement factory workers smoking behavior recognition
Some insights from 5 years of automated testing experience: UI automation must overcome these 10 pits
Redis Subscription and Redis Stream
飞桨开源社区季度报告来啦,你想知道的都在这里
数据链路层的数据传输
ofstream,ifstream,fstream读写文件
"NetEase Internship" Weekly Diary (2)
The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
YGG Guild Development Plan Season 1 Summary
Day116. Shangyitong: Details of appointment registration ※
LeetCode刷题日记:LCP 03.机器人大冒险
5年自动化测试经验的一些感悟:做UI自动化一定要跨过这10个坑
Huawei's 5-year female test engineer resigns: what a painful realization...
LeetCode brushing diary: 33. Search and rotate sorted array