-
Notifications
You must be signed in to change notification settings - Fork 4
/
netmanager.cpp
209 lines (169 loc) · 6.29 KB
/
netmanager.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
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
#include "stdafx.h"
#include "netmanager.h"
NetManager &NetManager::GetInstance()
{
static NetManager instance(NULL);
return instance;
}
NetManager::NetManager(QObject *parent)
: m_pManager(NULL), QObject(parent)
{
//// IE6
//m_request.setRawHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)");
m_pManager = new QNetworkAccessManager(this);
m_pManager->setCookieJar(new QNetworkCookieJar(this));
connect(m_pManager, SIGNAL(finished(QNetworkReply *)), this, SLOT(finished(QNetworkReply *)));
connect(m_pManager, SIGNAL(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)), this, SLOT(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)));
connect(m_pManager, SIGNAL(sslErrors(QNetworkReply *, const QList<QSslError> &)), this, SLOT(sslErrors(QNetworkReply *, const QList<QSslError> &)));
}
NetManager::~NetManager()
{
}
quint64 NetManager::GetTimeStamp10()
{
quint64 timeStamp = QDateTime::currentDateTime().toTime_t();
return timeStamp;
}
quint64 NetManager::GetTimeStamp13()
{
quint64 timeStamp = QDateTime::currentDateTime().toMSecsSinceEpoch();
return timeStamp;
}
QList<QNetworkCookie> NetManager::GetCookies(QString url)
{
QList<QNetworkCookie> cookies = m_pManager->cookieJar()->cookiesForUrl(QUrl(url));
return cookies;
}
void NetManager::GetUrl(QString url, QString referer)
{
//m_pManager->setCookieJar(NULL);
QUrl myUrl;
myUrl.setUrl(url);
QNetworkRequest request;
request.setUrl(myUrl);
request.setRawHeader("Accept", "application/json, text/javascript, application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*");
request.setRawHeader("Accept-Encoding", ""); //request.setRawHeader("Accept-Encoding", "gzip,deflate,sdch");
request.setRawHeader("Accept-Language", "zh-CN");
request.setRawHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
request.setRawHeader("Connection", "keep-alive");
request.setRawHeader("Referer", referer.toLocal8Bit());
request.setRawHeader("Host", myUrl.host().toLocal8Bit());
request.setRawHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)");
m_pManager->get(request);
}
void NetManager::GetUrl(QNetworkRequest request)
{
m_pManager->get(request);
}
void NetManager::PostUrl(QString url, QByteArray data, QString referer)
{
//m_pManager->setCookieJar(NULL);
QUrl myUrl;
myUrl.setUrl(url);
QNetworkRequest request;
request.setUrl(myUrl);
request.setRawHeader("Accept", "application/json, text/javascript, application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*");
request.setRawHeader("Accept-Encoding", ""); //request.setRawHeader("Accept-Encoding", "gzip,deflate,sdch");
request.setRawHeader("Accept-Language", "zh-CN");
request.setRawHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
request.setRawHeader("Connection", "keep-alive");
request.setRawHeader("Referer", referer.toLocal8Bit());
request.setRawHeader("Host", myUrl.host().toLocal8Bit());
request.setRawHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)");
m_pManager->post(request, data);
}
void NetManager::PostUrl(QNetworkRequest request, QByteArray data)
{
m_pManager->post(request, data);
}
void NetManager::GetUrlSync(QString url, QString referer)
{
GetUrl(url, referer);
QEventLoop eventLoop;
QTimer::singleShot(TIMEOUT_SECONDS, &eventLoop, SLOT(quit()));
QObject::connect(m_pManager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
eventLoop.exec(); // Block until finish
}
void NetManager::GetUrlSync(QNetworkRequest request)
{
GetUrl(request);
QEventLoop eventLoop;
QTimer::singleShot(TIMEOUT_SECONDS, &eventLoop, SLOT(quit()));
QObject::connect(m_pManager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
eventLoop.exec(); // Block until finish
}
void NetManager::PostUrlSync(QString url, QByteArray data, QString referer)
{
PostUrl(url, data, referer);
QEventLoop eventLoop;
QTimer::singleShot(TIMEOUT_SECONDS, &eventLoop, SLOT(quit()));
QObject::connect(m_pManager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
eventLoop.exec(); // Block until finish
}
void NetManager::PostUrlSync(QNetworkRequest request, QByteArray data)
{
PostUrl(request, data);
QEventLoop eventLoop;
QTimer::singleShot(TIMEOUT_SECONDS, &eventLoop, SLOT(quit()));
QObject::connect(m_pManager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
eventLoop.exec(); // Block until finish
}
QUrl NetManager::GetUrl()
{
return m_url;
}
QByteArray NetManager::GetContent()
{
return m_content;
}
void NetManager::finished(QNetworkReply *reply)
{
QNetworkReply::NetworkError error = reply->error();
if(error == QNetworkReply::NoError)
{
int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if(statusCode == 302 || statusCode == 301)
{
QString location = reply->rawHeader("Location");
if(location.startsWith("/"))
{
QUrl url;
url.setScheme(reply->url().scheme());
url.setHost(reply->url().host());
url.setPath(location);
location = url.toString();
}
GetUrl(location);
}
if(statusCode == 200)
{
//QString setCookie = reply->rawHeader("Set-Cookie");
m_url = reply->url();
m_content = reply->readAll();
//QFile logFile("C:\\log.html");
//if (logFile.open(QIODevice::Append | QIODevice::Text))
//{
// logFile.write(m_url.toString().toLocal8Bit());
// logFile.write("\n\n");
// logFile.write(m_content);
// logFile.write("\n\n\n\n--------------------------------------------------------------------------------\n\n\n\n");
// logFile.close();
//}
emit loadFinish(m_url, m_content);
}
}
else
{
m_url.clear();
m_content.clear();
}
reply->deleteLater();
}
void NetManager::networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility accessible)
{
//throw new exception("networkAccessibleChanged");
}
void NetManager::sslErrors(QNetworkReply * reply, const QList<QSslError> & errors)
{
//throw new exception("sslErrors");
}