-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.cpp
171 lines (160 loc) · 4.66 KB
/
request.cpp
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
/*******************************************************************
* 文件名称: request.cpp
* 简要描述: curl POST GET 请求模块
*
* 当前版本:1.0
* 作者: ZJJ
* 日期: 2020/11/29
* 说明: curl 请求打印模块
******************************************************************/
#include <iostream>
#include <curl/curl.h>
#include <string>
#include <map>
using namespace std;
static int write_data(char *buffer, size_t size, size_t nmemb, void *userp)
{
std::string *str = dynamic_cast<std::string *>((std::string *)userp);
str->append((char *)buffer, size * nmemb);
return nmemb;
}
int get(string url, string *response)
{
CURL *curl;
CURLcode ret;
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: Agent-007");
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, (char *)url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)response);
int status_code = curl_easy_perform(curl);
ret = curl_easy_perform(curl);
if (ret == 0)
{
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
return status_code;
}
else
{
curl_easy_cleanup(curl);
return ret;
}
}
else
{
return -1;
}
}
int posts(string url, string &body, string *response)
{
CURL *curl;
CURLcode ret;
curl = curl_easy_init();
long response_code = 0;
if (curl)
{
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, body.size());
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_URL, (char *)url.c_str());
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_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
curl_easy_cleanup(curl);
return 0;
}
else
{
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
curl_easy_cleanup(curl);
return ret;
}
}
else
{
return -1;
}
}
static size_t read_data(char *buffer, size_t size, size_t nitems, void *instream)
{
size_t sizes = fread(buffer, size, nitems, (FILE *)instream);
return nitems;
}
static int put(char *url, FILE *fd, int fsize, char *response)
{
CURL *curl;
CURLcode ret;
curl = curl_easy_init();
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "expect: ");
headers = curl_slist_append(headers, "test: put");
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, &read_data);
curl_easy_setopt(curl, CURLOPT_READDATA, fd);
curl_easy_setopt(curl, CURLOPT_INFILE, fd);
curl_easy_setopt(curl, CURLOPT_INFILESIZE, fsize);
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
{
curl_easy_cleanup(curl);
return ret;
}
}
else
{
return -1;
}
}
static int post(string url, string &body, string *response)
{
CURL *curl;
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, (char *)body.c_str());
curl_easy_setopt(curl, CURLOPT_URL, (char *)url.c_str());
curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
else
{
cout << "error" << endl;
}
return 0;
}
static void makeUrlencodedForm(map<string, string> const ¶ms, string *content)
{
content->clear();
map<string, string>::const_iterator it;
for (it = params.begin(); it != params.end(); it++)
{
char *key = curl_escape(it->first.c_str(), (int)it->first.size());
char *value = curl_escape(it->second.c_str(), (int)it->second.size());
*content += key;
*content += '=';
*content += value;
*content += '&';
curl_free(key);
curl_free(value);
}
}