当前位置:网站首页>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
边栏推荐
- Check if IP or port is blocked
- Anti-oversold and high concurrent deduction scheme for e-commerce inventory system
- LeetCode刷题日记:74. 搜索二维矩阵
- Redis Persistence - RDB and AOF
- typescript29-枚举类型的特点和原理
- Garbage Collector CMS and G1
- typescript33-typescript高级概述
- Shell Beginners Final Chapter
- 【LeetCode每日一题】——704.二分查找
- 雇用WordPress开发人员:4个实用的方法
猜你喜欢
随机推荐
ofstream,ifstream,fstream读写文件
求大神解答,这种 sql 应该怎么写?
Yunhe Enmo: Let the value of the commercial database era continue to prosper in the openGauss ecosystem
Redis 持久化 - RDB 与 AOF
Byte taught me a hard lesson: When a crisis comes, you don't even have time to prepare...
LeetCode Review Diary: 153. Find the Minimum Value in a Rotated Sort Array
Use baidu EasyDL implement factory workers smoking behavior recognition
成都openGauss用户组招募啦!
Day116.尚医通:预约挂号详情 ※
Hiring a WordPress Developer: 4 Practical Ways
『网易实习』周记(三)
Redis Subscription and Redis Stream
雇用WordPress开发人员:4个实用的方法
『网易实习』周记(二)
【LeetCode每日一题】——704.二分查找
Newton's theorem and related corollaries
kubernetes之服务发现
AOF重写
LeetCode brushing diary: 33. Search and rotate sorted array
typescript36-class的构造函数实例方法