forked from chronolaw/cpp_study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurl.cpp
More file actions
94 lines (71 loc) · 1.9 KB
/
curl.cpp
File metadata and controls
94 lines (71 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2020 by Chrono
//
// sudo apt-get remove libcurl4
// sudo apt-get install libcurl4-openssl-dev
//
// curl-config --libs
// curl-config --version
// curl-config --features
// curl-config --protocols
//
// g++ curl.cpp -std=c++11 -lcurl -o a.out;./a.out
// g++ curl.cpp -std=c++14 -lcurl -o a.out;./a.out
#include <cassert>
#include <iostream>
#include <curl/curl.h>
using namespace std;
// in curl.h
size_t write_callback(char* ptr, size_t size, size_t nmemb, void* userdata);
void case1()
{
auto curl = curl_easy_init();
assert(curl);
curl_easy_setopt(curl, CURLOPT_URL, "http://nginx.org");
auto res = curl_easy_perform(curl);
if (res != CURLE_OK) {
cout << curl_easy_strerror(res) << endl;
}
curl_easy_cleanup(curl);
}
void case2()
{
//using curl_t = CURL;
auto curl = curl_easy_init();
if (!curl) {
cout << "curl init error" << endl;
return;
}
curl_easy_setopt(curl, CURLOPT_URL, "http://nginx.org");
//curl_easy_setopt(curl, CURLOPT_WRITEDATA, NULL);
#if 0
decltype(&write_callback) f =
[](char *ptr, size_t size, size_t nmemb, void *userdata)
{
cout << "size = " << size * nmemb << endl;
return size * nmemb;
};
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, f);
#endif
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
(decltype(&write_callback))
[](char *ptr, size_t size, size_t nmemb, void *userdata)
{
cout << "size = " << size * nmemb << endl;
return size * nmemb;
}
);
auto res = curl_easy_perform(curl);
if (res != CURLE_OK) {
cout << curl_easy_strerror(res) << endl;
}
curl_easy_cleanup(curl);
}
int main()
{
curl_global_init(CURL_GLOBAL_SSL);
//case1();
case2();
cout << curl_version() << endl;
cout << "libcurl demo" << endl;
curl_global_cleanup();
}