-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpclient.cpp
More file actions
391 lines (390 loc) · 17.8 KB
/
Copy pathhttpclient.cpp
File metadata and controls
391 lines (390 loc) · 17.8 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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include "HttpClient.h"
#include <QDebug>
#include <QFile>
#include <QHash>
#include <QUrlQuery>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QNetworkAccessManager>
#include <QHttpPart>
#include <QHttpMultiPart>
class HttpClientPrivate {
public:
HttpClientPrivate(const QString &url);
QString url; // 请求的 URL
QUrlQuery params; // 请求的参数使用 Form 格式
QString json; // 请求的参数使用 Json 格式
QHash<QString, QString> headers; // 请求头
QNetworkAccessManager *manager;
bool useJson; // 为 true 时请求使用 Json 格式传递参数,否则使用 Form 格式传递参数
bool debug; // 为 true 时输出请求的 URL 和参数
// HTTP 请求的类型
enum HttpMethod {
GET, POST, PUT, DELETE, UPLOAD /* UPLOAD 不是 HTTP Method,只是为了上传时特殊处理而定义的 */
};
/**
* @brief 获取 Manager,如果使用传入的 manager 则返回此 manager,否则新创建一个 manager
* @param d HttpClientPrivate 的对象
* @param internal 使用传入的 manager 则 interval 被设置为 false,创建新的 manager 则设置 interval 为 true
* @return 返回 QNetworkAccessManager 对象
*/
static QNetworkAccessManager* getManager(HttpClientPrivate *d, bool *internal);
/**
* @brief 使用用户设定的 URL、请求头等创建 Request
* @param d HttpClientPrivate 的对象
* @param method 请求的类型
* @return 返回可用于执行请求的 QNetworkRequest
*/
static QNetworkRequest createRequest(HttpClientPrivate *d, HttpMethod method);
/**
* @brief 执行请求的辅助函数
* @param d HttpClient 的辅助对象
* @param method 请求的类型
* @param successHandler 请求成功的回调 lambda 函数
* @param errorHandler 请求失败的回调 lambda 函数
* @param encoding 请求响应的编码
*/
static void executeQuery(HttpClientPrivate *d, HttpMethod method,
std::function<void (const QString &)> successHandler,
std::function<void (const QString &)> errorHandler,
const char *encoding);
/**
* @brief 上传文件或者数据
* @param d HttpClientPrivate 的对象
* @param path 要上传的文件的路径(path 和 data 不能同时使用)
* @param data 要上传的文件的数据
* @param successHandler 请求成功的回调 lambda 函数
* @param errorHandler 请求失败的回调 lambda 函数
* @param encoding 请求响应的编码
*/
static void upload(HttpClientPrivate *d,
const QString &path, const QByteArray &data,
std::function<void (const QString &)> successHandler,
std::function<void (const QString &)> errorHandler,
const char *encoding);
/**
* @brief 读取服务器响应的数据
* @param reply 请求的 QNetworkReply 对象
* @param encoding 请求响应的编码,默认使用 UTF-8
* @return 服务器端响应的字符串
*/
static QString readReply(QNetworkReply *reply, const char *encoding = "UTF-8");
/**
* @brief 请求结束的处理函数
* @param debug 如果为 true 则输出调试信息,为 false 不输出
* @param successMessage 请求成功的消息
* @param errorMessage 请求失败的消息
* @param successHandler 请求成功的回调 lambda 函数
* @param errorHandler 请求失败的回调 lambda 函数
* @param reply QNetworkReply 对象,不能为 NULL
* @param manager 请求的 manager,不为 NULL 时在此函数中 delete
*/
static void handleFinish(bool debug,
const QString &successMessage,
const QString &errorMessage,
std::function<void (const QString &)> successHandler,
std::function<void (const QString &)> errorHandler,
QNetworkReply *reply, QNetworkAccessManager *manager);
};
HttpClientPrivate::HttpClientPrivate(const QString &url) : url(url), manager(NULL), useJson(false), debug(false) {
}
// 注意: 不要在回调函数中使用 d,因为回调函数被调用时 HttpClient 对象很可能已经被释放掉了。
HttpClient::HttpClient(const QString &url) : d(new HttpClientPrivate(url)) {
}
HttpClient::~HttpClient() {
delete d;
}
HttpClient &HttpClient::manager(QNetworkAccessManager *manager) {
d->manager = manager;
return *this;
}
// 传入 debug 为 true 则使用 debug 模式,请求执行时输出请求的 URL 和参数等
HttpClient &HttpClient::debug(bool debug) {
d->debug = debug;
return *this;
}
// 添加 Form 格式参数
HttpClient &HttpClient::param(const QString &name, const QString &value) {
d->params.addQueryItem(name, value);
return *this;
}
// 添加 Json 格式参数
HttpClient &HttpClient::json(const QString &json) {
d->useJson = true;
d->json = json;
return *this;
}
// 添加访问头
HttpClient &HttpClient::header(const QString &header, const QString &value) {
d->headers[header] = value;
return *this;
}
// 执行 GET 请求
void HttpClient::get(std::function<void (const QString &)> successHandler,
std::function<void (const QString &)> errorHandler,
const char *encoding) {
HttpClientPrivate::executeQuery(d, HttpClientPrivate::GET, successHandler, errorHandler, encoding);
}
// 执行 POST 请求
void HttpClient::post(std::function<void (const QString &)> successHandler,
std::function<void (const QString &)> errorHandler,
const char *encoding) {
HttpClientPrivate::executeQuery(d, HttpClientPrivate::POST, successHandler, errorHandler, encoding);
}
// 执行 PUT 请求
void HttpClient::put(std::function<void (const QString &)> successHandler,
std::function<void (const QString &)> errorHandler,
const char *encoding) {
HttpClientPrivate::executeQuery(d, HttpClientPrivate::PUT, successHandler, errorHandler, encoding);
}
// 执行 DELETE 请求
void HttpClient::remove(std::function<void (const QString &)> successHandler,
std::function<void (const QString &)> errorHandler,
const char *encoding) {
HttpClientPrivate::executeQuery(d, HttpClientPrivate::DELETE, successHandler, errorHandler, encoding);
}
void HttpClient::download(const QString &destinationPath,
std::function<void (const QString &)> successHandler,
std::function<void (const QString &)> errorHandler) {
bool debug = d->debug;
QFile *file = new QFile(destinationPath);
if (file->open(QIODevice::WriteOnly)) {
download([=](const QByteArray &data) {
file->write(data);
}, [=](const QString &) {
// 请求结束后释放文件对象
file->flush();
file->close();
file->deleteLater();
if (debug) {
qDebug().noquote() << QString("下载完成,保存到: %1").arg(destinationPath);
}
if (NULL != successHandler) {
successHandler(QString("下载完成,保存到: %1").arg(destinationPath));
}
}, errorHandler);
} else {
// 打开文件出错
if (debug) {
qDebug().noquote() << QString("打开文件出错: %1").arg(destinationPath);
}
if (NULL != errorHandler) {
errorHandler(QString("打开文件出错: %1").arg(destinationPath));
}
}
}
// 使用 GET 进行下载,当有数据可读取时回调 readyRead(), 大多数情况下应该在 readyRead() 里把数据保存到文件
void HttpClient::download(std::function<void (const QByteArray &)> readyRead,
std::function<void (const QString &)> successHandler,
std::function<void (const QString &)> errorHandler) {
bool debug = d->debug;
bool internal;
QNetworkAccessManager *manager = HttpClientPrivate::getManager(d, &internal);
QNetworkRequest request = HttpClientPrivate::createRequest(d, HttpClientPrivate::GET);
QNetworkReply *reply = manager->get(request);
// 有数据可读取时回调 readyRead()
QObject::connect(reply, &QNetworkReply::readyRead, [=] {
readyRead(reply->readAll());
});
// 请求结束
QObject::connect(reply, &QNetworkReply::finished, [=] {
QString successMessage = "下载完成"; // 请求结束时一次性读取所有响应数据
QString errorMessage = reply->errorString();
HttpClientPrivate::handleFinish(debug, successMessage, errorMessage, successHandler, errorHandler,
reply, internal ? manager : NULL);
});
}
// 上传文件
void HttpClient::upload(const QString &path,
std::function<void (const QString &)> successHandler,
std::function<void (const QString &)> errorHandler,
const char *encoding) {
HttpClientPrivate::upload(d, path, QByteArray(), successHandler, errorHandler, encoding);
}
// 上传数据
void HttpClient::upload(const QByteArray &data,
std::function<void (const QString &)> successHandler,
std::function<void (const QString &)> errorHandler,
const char *encoding) {
HttpClientPrivate::upload(d, QString(), data, successHandler, errorHandler, encoding);
}
// 上传文件或者数据的实现
void HttpClientPrivate::upload(HttpClientPrivate *d,
const QString &path, const QByteArray &data,
std::function<void (const QString &)> successHandler,
std::function<void (const QString &)> errorHandler,
const char *encoding) {
bool debug = d->debug;
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
// 创建 Form 表单的参数 Text Part
QList<QPair<QString, QString> > paramItems = d->params.queryItems();
for (int i = 0; i < paramItems.size(); ++i) {
QHttpPart textPart;
QString name = paramItems.at(i).first;
QString value = paramItems.at(i).second;
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QString("form-data; name=\"%1\"").arg(name));
textPart.setBody(value.toUtf8());
multiPart->append(textPart);
}
if (!path.isEmpty()) {
// path 不为空时,上传文件
QFile *file = new QFile(path);
file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart
// 如果文件打开失败,则释放资源返回
if(!file->open(QIODevice::ReadOnly)) {
QString errorMessage = QString("打开文件失败[%2]: %1").arg(path).arg(file->errorString());
if (debug) {
qDebug().noquote() << errorMessage;
}
if (NULL != errorHandler) {
errorHandler(errorMessage);
}
multiPart->deleteLater();
return;
}
// 文件上传的参数名为 file,值为文件名
QString disposition = QString("form-data; name=\"file\"; filename=\"%1\"").arg(file->fileName());
QHttpPart filePart;
filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(disposition));
filePart.setBodyDevice(file);
multiPart->append(filePart);
} else {
// 上传数据
QString disposition = QString("form-data; name=\"file\"; filename=\"no-name\"");
QHttpPart dataPart;
dataPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(disposition));
dataPart.setBody(data);
multiPart->append(dataPart);
}
bool internal;
QNetworkAccessManager *manager = HttpClientPrivate::getManager(d, &internal);
QNetworkRequest request = HttpClientPrivate::createRequest(d, HttpClientPrivate::UPLOAD);
QNetworkReply *reply = manager->post(request, multiPart);
QObject::connect(reply, &QNetworkReply::finished, [=] {
multiPart->deleteLater(); // 释放资源: multiPart + file
QString successMessage = HttpClientPrivate::readReply(reply, encoding); // 请求结束时一次性读取所有响应数据
QString errorMessage = reply->errorString();
HttpClientPrivate::handleFinish(debug, successMessage, errorMessage, successHandler, errorHandler,
reply, internal ? manager : NULL);
});
}
// 执行请求的辅助函数
void HttpClientPrivate::executeQuery(HttpClientPrivate *d, HttpMethod method,
std::function<void (const QString &)> successHandler,
std::function<void (const QString &)> errorHandler,
const char *encoding) {
// 如果不使用外部的 manager 则创建一个新的,在访问完成后会自动删除掉
bool debug = d->debug;
bool internal;
QNetworkAccessManager *manager = HttpClientPrivate::getManager(d, &internal);
QNetworkRequest request = HttpClientPrivate::createRequest(d, method);
QNetworkReply *reply = NULL;
switch (method) {
case HttpClientPrivate::GET:
reply = manager->get(request);
break;
case HttpClientPrivate::POST:
reply = manager->post(request, d->useJson ? d->json.toUtf8() : d->params.toString(QUrl::FullyEncoded).toUtf8());
break;
case HttpClientPrivate::PUT:
reply = manager->put(request, d->useJson ? d->json.toUtf8() : d->params.toString(QUrl::FullyEncoded).toUtf8());
break;
case HttpClientPrivate::DELETE:
reply = manager->deleteResource(request);
break;
default:
break;
}
QObject::connect(reply, &QNetworkReply::finished, [=] {
QString successMessage = HttpClientPrivate::readReply(reply, encoding); // 请求结束时一次性读取所有响应数据
QString errorMessage = reply->errorString();
HttpClientPrivate::handleFinish(debug, successMessage, errorMessage, successHandler, errorHandler,
reply, internal ? manager : NULL);
});
}
QNetworkAccessManager* HttpClientPrivate::getManager(HttpClientPrivate *d, bool *internal) {
*internal = d->manager == NULL;
return *internal ? new QNetworkAccessManager() : d->manager;
}
QNetworkRequest HttpClientPrivate::createRequest(HttpClientPrivate *d, HttpMethod method) {
bool get = method == HttpMethod::GET;
bool upload = method == HttpClientPrivate::UPLOAD;
bool postForm = !get && !upload && !d->useJson;
bool postJson = !get && !upload && d->useJson;
// 如果是 GET 请求,并且参数不为空,则编码请求的参数,放到 URL 后面
if (get && !d->params.isEmpty()) {
d->url += "?" + d->params.toString(QUrl::FullyEncoded);
}
// 调试时输出网址和参数
if (d->debug) {
qDebug().noquote() << "网址:" << d->url;
if (postJson) {
qDebug().noquote() << "参数:" << d->json;
} else if (postForm || upload) {
QList<QPair<QString, QString> > paramItems = d->params.queryItems();
// 按键值对的方式输出参数
for (int i = 0; i < paramItems.size(); ++i) {
QString name = paramItems.at(i).first;
QString value = paramItems.at(i).second;
if (0 == i) {
qDebug().noquote() << QString("参数: %1=%2").arg(name).arg(value);
} else {
qDebug().noquote() << QString(" %1=%2").arg(name).arg(value);
}
}
}
}
// 如果是 POST 请求,useJson 为 true 时添加 Json 的请求头,useJson 为 false 时添加 Form 的请求头
if (postForm) {
d->headers["Content-Type"] = "application/x-www-form-urlencoded";
} else if (postJson) {
d->headers["Accept"] = "application/json; charset=utf-8";
d->headers["Content-Type"] = "application/json";
}
// 把请求的头添加到 request 中
QNetworkRequest request(QUrl(d->url));
QHashIterator<QString, QString> iter(d->headers);
while (iter.hasNext()) {
iter.next();
request.setRawHeader(iter.key().toUtf8(), iter.value().toUtf8());
}
return request;
}
QString HttpClientPrivate::readReply(QNetworkReply *reply, const char *encoding) {
QTextStream in(reply);
QString result;
in.setCodec(encoding);
while (!in.atEnd()) {
result += in.readLine();
}
return result;
}
void HttpClientPrivate::handleFinish(bool debug,
const QString &successMessage,
const QString &errorMessage,
std::function<void (const QString &)> successHandler,
std::function<void (const QString &)> errorHandler,
QNetworkReply *reply, QNetworkAccessManager *manager) {
if (reply->error() == QNetworkReply::NoError) {
// 请求成功
if (debug) {
qDebug().noquote() << QString("[成功]请求结束: %1").arg(successMessage);
}
if (NULL != successHandler) {
successHandler(successMessage);
}
} else {
// 请求失败
if (debug) {
qDebug().noquote() << QString("[失败]请求结束: %1").arg(errorMessage);
}
if (NULL != errorHandler) {
errorHandler(errorMessage);
}
}
// 释放资源
reply->deleteLater();
if (NULL != manager) {
manager->deleteLater();
}
}