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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <fstream>
#include "CommonTools.h"
using namespace std;
std::string string_To_UTF8(const std::string& str)
{
int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);
wchar_t* pwBuf = new wchar_t[nwLen + 1];//一定要加1,不然会出现尾巴
ZeroMemory(pwBuf, nwLen * 2 + 2);
::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);
int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);
char* pBuf = new char[nLen + 1];
ZeroMemory(pBuf, nLen + 1);
::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);
std::string retStr(pBuf);
delete[]pwBuf;
delete[]pBuf;
pwBuf = NULL;
pBuf = NULL;
return retStr;
}
int write_data(void* buffer, int size, int nmemb, void* userp) {
std::string* str = dynamic_cast<std::string*>((std::string*)userp);
str->append((char*)buffer, size * nmemb);
return nmemb;
}
size_t CommonTools::receive_data(void* contents, size_t size, size_t nmemb, void* stream) {
string* str = (string*)stream;
(*str).append((char*)contents, size * nmemb);
return size * nmemb;
}
size_t CommonTools::writedata2file(void* ptr, size_t size, size_t nmemb, FILE* stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
int CommonTools::upload_file(const char* url, const char* fileName, string& response) {
CURL* curl;
CURLcode ret;
curl = curl_easy_init();
struct curl_httppost* post = NULL;
struct curl_httppost* last = NULL;
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, url); //指定url
curl_formadd(&post, &last, CURLFORM_PTRNAME, "path", CURLFORM_PTRCONTENTS, "device_cover", CURLFORM_END);//form-data key(path) 和 value(device_cover)
curl_formadd(&post, &last, CURLFORM_PTRNAME, "file", CURLFORM_FILE, fileName, CURLFORM_FILENAME, "hello.jpg", CURLFORM_END);// form-data key(file) "./test.jpg"为文件路径 "hello.jpg" 为文件上传时文件名
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post); //构造post参数
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 3);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); //绑定相应
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&response); //绑定响应内容的地址
ret = curl_easy_perform(curl); //执行请求
if (ret == 0) {
curl_easy_cleanup(curl);
return 0;
}
else {
return ret;
}
}
else {
return -1;
}
//std::string contents;
//std::ifstream in(fileName, std::ios::in | std::ios::binary);
//if (in)
//{
// in.seekg(0, std::ios::end);
// contents.resize(in.tellg());
// in.seekg(0, std::ios::beg);
// in.read(&contents[0], contents.size());
// in.close();
//}
//CURL* curl;
//CURLcode res;
//struct curl_httppost* formpost = NULL;
//struct curl_httppost* lastptr = NULL;
//struct curl_slist* headerlist = NULL;
//static const char buf[] = "Expect:";
//curl_global_init(CURL_GLOBAL_ALL);
//// set up the header
//curl_formadd(&formpost,
// &lastptr,
// CURLFORM_COPYNAME, "cache-control:",
// CURLFORM_COPYCONTENTS, "no-cache",
// CURLFORM_END);
//curl_formadd(&formpost,
// &lastptr,
// CURLFORM_COPYNAME, "content-type:",
// CURLFORM_COPYCONTENTS, "multipart/form-data",
// CURLFORM_END);
//curl_formadd(&formpost, &lastptr,
// CURLFORM_COPYNAME, "file", // <--- the (in this case) wanted file-Tag!
// CURLFORM_BUFFER, fileName,
// CURLFORM_BUFFERPTR, contents.data(),
// CURLFORM_BUFFERLENGTH, contents.size(),
// CURLFORM_END);
//curl = curl_easy_init();
//headerlist = curl_slist_append(headerlist, buf);
//if (curl) {
// curl_easy_setopt(curl, CURLOPT_URL, url);
// curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
// curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
// curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&response);
// //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
// res = curl_easy_perform(curl);
// /* Check for errors */
// if (res != CURLE_OK)
// fprintf(stderr, "curl_easy_perform() failed: %s\n",
// curl_easy_strerror(res));
// curl_easy_cleanup(curl);
// curl_formfree(formpost);
// curl_slist_free_all(headerlist);
//}
//return 0;
}
int CommonTools::download_file(const char* url, const char outfilename[FILENAME_MAX]) {
CURL* curl;
FILE* fp;
CURLcode res;
/* 调用curl_global_init()初始化libcurl */
res = curl_global_init(CURL_GLOBAL_ALL);
if (CURLE_OK != res)
{
printf("init libcurl failed.");
curl_global_cleanup();
return -1;
}
/* 调用curl_easy_init()函数得到 easy interface型指针 */
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename, "wb");
/* 调用curl_easy_setopt()设置传输选项 */
res = curl_easy_setopt(curl, CURLOPT_URL, url);
if (res != CURLE_OK)
{
fclose(fp);
curl_easy_cleanup(curl);
return -1;
}
/* 根据curl_easy_setopt()设置的传输选项,实现回调函数以完成用户特定任务 */
res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CommonTools::writedata2file);
if (res != CURLE_OK) {
fclose(fp);
curl_easy_cleanup(curl);
return -1;
}
/* 根据curl_easy_setopt()设置的传输选项,实现回调函数以完成用户特定任务 */
res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
if (res != CURLE_OK)
{
fclose(fp);
curl_easy_cleanup(curl);
return -1;
}
res = curl_easy_perform(curl);
// 调用curl_easy_perform()函数完成传输任务
fclose(fp);
/* Check for errors */
if (res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
curl_easy_cleanup(curl);
return -1;
}
/* always cleanup */
curl_easy_cleanup(curl);
// 调用curl_easy_cleanup()释放内存
}
curl_global_cleanup();
return 0;
}
CURLcode CommonTools::HttpGet(const std::string& strUrl, std::string& strResponse, int nTimeout) {
CURLcode res;
CURL* pCURL = curl_easy_init();
if (pCURL == NULL) {
return CURLE_FAILED_INIT;
}
curl_easy_setopt(pCURL, CURLOPT_URL, strUrl.c_str());
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(pCURL, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(pCURL, CURLOPT_TIMEOUT, nTimeout);
curl_easy_setopt(pCURL, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(pCURL, CURLOPT_WRITEFUNCTION, CommonTools::receive_data);
curl_easy_setopt(pCURL, CURLOPT_WRITEDATA, (void*)&strResponse);
res = curl_easy_perform(pCURL);
curl_easy_cleanup(pCURL);
return res;
}
CURLcode CommonTools::HttpPost(const std::string& strUrl, std::string szJson, std::string& strResponse, int nTimeout) {
CURLcode res;
char szJsonData[1024];
memset(szJsonData, 0, sizeof(szJsonData));
strcpy(szJsonData, szJson.c_str());
CURL* pCURL = curl_easy_init();
struct curl_slist* headers = NULL;
if (pCURL == NULL) {
return CURLE_FAILED_INIT;
}
CURLcode ret;
ret = curl_easy_setopt(pCURL, CURLOPT_URL, strUrl.c_str());
// std::cout << ret << std::endl;
ret = curl_easy_setopt(pCURL, CURLOPT_POST, 1L);
headers = curl_slist_append(headers, "content-type:application/json");
ret = curl_easy_setopt(pCURL, CURLOPT_HTTPHEADER, headers);
ret = curl_easy_setopt(pCURL, CURLOPT_POSTFIELDS, szJsonData);
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
ret = curl_easy_setopt(pCURL, CURLOPT_TIMEOUT, nTimeout);
ret = curl_easy_setopt(pCURL, CURLOPT_WRITEFUNCTION, CommonTools::receive_data);
ret = curl_easy_setopt(pCURL, CURLOPT_WRITEDATA, (void*)&strResponse);
res = curl_easy_perform(pCURL);
curl_easy_cleanup(pCURL);
return res;
}
CURLcode CommonTools::HttpPut(const std::string& strUrl, std::string szJson, std::string& strResponse, int nTimeout) {
CURLcode res;
char szJsonData[1024];
memset(szJsonData, 0, sizeof(szJsonData));
strcpy(szJsonData, szJson.c_str());
CURL* pCURL = curl_easy_init();
struct curl_slist* headers = NULL;
if (pCURL == NULL) {
return CURLE_FAILED_INIT;
}
CURLcode ret;
ret = curl_easy_setopt(pCURL, CURLOPT_URL, strUrl.c_str());
// std::cout << ret << std::endl;
ret = curl_easy_setopt(pCURL, CURLOPT_CUSTOMREQUEST, "PUT");
headers = curl_slist_append(headers, "content-type:application/json");
ret = curl_easy_setopt(pCURL, CURLOPT_HTTPHEADER, headers);
ret = curl_easy_setopt(pCURL, CURLOPT_POSTFIELDS, szJsonData);
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
ret = curl_easy_setopt(pCURL, CURLOPT_TIMEOUT, nTimeout);
ret = curl_easy_setopt(pCURL, CURLOPT_WRITEFUNCTION, CommonTools::receive_data);
ret = curl_easy_setopt(pCURL, CURLOPT_WRITEDATA, (void*)&strResponse);
res = curl_easy_perform(pCURL);
curl_easy_cleanup(pCURL);
return res;
}
CURLcode CommonTools::HttpDelete(const std::string& strUrl, std::string& strResponse, int nTimeout) {
CURLcode res;
CURL* pCURL = curl_easy_init();
struct curl_slist* headers = NULL;
if (pCURL == NULL) {
return CURLE_FAILED_INIT;
}
CURLcode ret;
ret = curl_easy_setopt(pCURL, CURLOPT_URL, strUrl.c_str());
// std::cout << ret << std::endl;
ret = curl_easy_setopt(pCURL, CURLOPT_CUSTOMREQUEST, "DELETE");
headers = curl_slist_append(headers, "content-type:application/json");
ret = curl_easy_setopt(pCURL, CURLOPT_HTTPHEADER, headers);
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
ret = curl_easy_setopt(pCURL, CURLOPT_TIMEOUT, nTimeout);
ret = curl_easy_setopt(pCURL, CURLOPT_WRITEFUNCTION, CommonTools::receive_data);
ret = curl_easy_setopt(pCURL, CURLOPT_WRITEDATA, (void*)&strResponse);
res = curl_easy_perform(pCURL);
curl_easy_cleanup(pCURL);
return res;
}