-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_curl_simple.cpp
More file actions
38 lines (30 loc) · 1.18 KB
/
Copy pathtest_curl_simple.cpp
File metadata and controls
38 lines (30 loc) · 1.18 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
#include <iostream>
#include <curl/curl.h>
#include <string>
size_t write_callback(void *contents, size_t size, size_t nmemb, void *userp) {
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
int main() {
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
std::string readBuffer;
// 测试简单的 HTTP GET 请求
curl_easy_setopt(curl, CURLOPT_URL, "https://httpbin.org/get");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L);
std::cout << "Making HTTP request..." << std::endl;
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
} else {
std::cout << "Request successful, received " << readBuffer.length() << " bytes" << std::endl;
std::cout << "First 100 chars: " << readBuffer.substr(0, 100) << std::endl;
}
curl_easy_cleanup(curl);
}
return 0;
}