blob: f3d8daac2d659fe0c9dc2550a352e629cbd8f40f (
plain) (
blame)
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
|
#include "webclient.hpp"
#include <QNetworkRequest>
#include <QNetworkReply>
WebClient::WebClient(QObject *parent) :
QObject(parent),
current_reply(nullptr)
{
manager.setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
}
WebClient::~WebClient()
{
}
bool WebClient::startRequest(const QUrl &url)
{
if(url.scheme() != "http" and url.scheme() != "https")
return false;
if(this->current_reply != nullptr)
return true;
this->body.clear();
QNetworkRequest request(url);
request.setMaximumRedirectsAllowed(5);
request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
this->current_reply = manager.get(request);
if(this->current_reply == nullptr)
return false;
connect(this->current_reply, &QNetworkReply::readyRead, this, &WebClient::on_data);
connect(this->current_reply, &QNetworkReply::finished, this, &WebClient::on_finished);
return true;
}
bool WebClient::isInProgress() const
{
return (this->current_reply != nullptr);
}
bool WebClient::cancelRequest()
{
if(this->current_reply != nullptr)
{
this->current_reply->abort();
this->current_reply = nullptr;
}
this->body.clear();
return true;
}
void WebClient::on_data()
{
this->body.append(this->current_reply->readAll());
}
void WebClient::on_finished()
{
if(this->current_reply->error() != QNetworkReply::NoError)
{
emit this->requestFailed(this->current_reply->errorString());
}
else
{
auto mime = this->current_reply->header(QNetworkRequest::ContentTypeHeader).toString();
qDebug() << this->current_reply->url() << mime;
emit this->requestComplete(this->body, mime);
this->body.clear();
}
this->current_reply->deleteLater();
this->current_reply = nullptr;
}
|