-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttprequest.h
81 lines (69 loc) · 2.43 KB
/
httprequest.h
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
#ifndef REQUESTSHANDLER_H
#define REQUESTSHANDLER_H
/**
* This class is designed for single thread access without blocking.
* it is thread un safe. (no parralel or concurrent modification will be handled.)
*
*
*
* */
#include <QObject>
#include <QUrl>
#include <QUrlQuery>
#include <QMap>
#include <QThread>
#include <QMutex>
#include <QMutexLocker>
#include <QDebug>
#include <QNetworkSession>
#include <QtNetwork/QNetworkConfigurationManager>
#include <QNetworkAccessManager>
class HttpRequest : public QThread
{
Q_OBJECT
public:
explicit HttpRequest(QObject *parent = nullptr);
~HttpRequest();
void newRequest(QUrl *url,QString *requestMethod,
QMap<QString,QString> *headers, QUrlQuery *urlQuery);
void newRequest( QUrl *url, QString *requestMethod,
QMap<QString,QString> *headers, QByteArray* body);
void newRequest( QUrl *url, QString *requestMethod,
QMap<QString,QString> *headers, QByteArray* body, QUrlQuery *urlQuery);
void start();
void cancelRequest();
void terminateBlocking();
bool openConnection(const std::function<void(bool)> callback) {
auto networkManager{ QNetworkConfigurationManager{this}};
const bool canStartIAP = (networkManager.capabilities() &
QNetworkConfigurationManager::CanStartAndStopInterfaces);
// default access point, use it
QNetworkConfiguration cfg = networkManager.defaultConfiguration();
if (!cfg.isValid() || (!canStartIAP && cfg.state() !=
QNetworkConfiguration::Active))
{
callback(false);
return false;
}
QNetworkSession* session = new QNetworkSession(cfg, this);
session->open();
return session->waitForOpened(-1);
}
private:
QUrl *url = nullptr;
QString *requestMethod = nullptr;
QMap<QString , QString> *headers = nullptr;
QByteArray *body = nullptr;
QUrlQuery *urlQueryParams = nullptr;
QNetworkReply* reply = nullptr;
signals:
void requestFinished(bool error,
int statusCode,
const QString& errorString,
const QByteArray& response,
const QList<QPair<QByteArray,QByteArray>>& responseHeaders,
const QList<QNetworkCookie>& cookies);
protected:
void run() override;
};
#endif // REQUESTSHANDLER_H