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
95
96
97
98
99
100
| #include <curl/curl.h>
#include <string>
#include <iostream>
using namespace std;
int httpPutFile() {
CURL* curl = NULL;
CURLcode result = CURLE_OK;
string url = "http://10.200.100.50:8080";
FILE* inputFile = fopen("./put.txt", "rb");
string outputFilename = "/1.txt";
url += outputFilename;
//HttpHead
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "endPoint:http://10.200.100.50:8080");
headers = curl_slist_append(headers, "accessKeyId:id123");
headers = curl_slist_append(headers, "secretAccessKey:key123");
headers = curl_slist_append(headers, "Authorization:auth123");
//Init
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
//Option
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 2);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_INFILE, inputFile);
//curl_easy_setopt(curl, CURLOPT_INFILESIZE, 87);
curl_easy_setopt(curl, CURLOPT_PUT, 1);
//curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
//Perform
result = curl_easy_perform(curl);
//Clean
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
curl_global_cleanup();
return 0;
}
int httpGetFile() {
CURL* curl = NULL;
CURLcode result = CURLE_OK;
string url = "http://10.200.100.50:8080";
FILE* outputFile = fopen("./get.txt", "wb");
string inputFilename = "/1.txt";
url += inputFilename;
//HttpHead
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "endPoint:http://10.200.100.50:8080");
headers = curl_slist_append(headers, "accessKeyId:id123");
headers = curl_slist_append(headers, "secretAccessKey:key123");
headers = curl_slist_append(headers, "Authorization:auth123");
//Init
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
//Option
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 2);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)outputFile);
//Perform
result = curl_easy_perform(curl);
//Clean
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
curl_global_cleanup();
return 0;
}
int main(int argc, char* argv[]) {
string method = argv[1];
if(method == "PUT" || method == "put") {
httpPutFile();
}else if(method == "GET" || method == "get") {
httpGetFile();
}else {
cout << "Method error, please input \"PUT\" or \"GET\"." << endl;
}
cout << endl;
return 0;
}
|