@@ -8,28 +8,33 @@ HttpTransceiver::HttpTransceiver(QWidget *parent) :
8
8
QDialog(parent),
9
9
ui(new Ui::HttpTransceiver),
10
10
m_netManager(new QNetworkAccessManager(this )),
11
+ m_downloadFile(new QTemporaryFile),
11
12
m_reply(nullptr )
12
13
{
13
14
ui->setupUi (this );
14
15
15
16
this ->setWindowTitle (" HTTP Request" );
17
+ m_downloadFile->open ();
16
18
17
19
setDownloadMode ();
18
20
connect (
19
21
m_netManager,
20
22
&QNetworkAccessManager::finished,
21
23
this ,
22
24
&HttpTransceiver::replyReceived);
25
+
26
+ connect (ui->pb_interrupt , &QPushButton::clicked, this , &HttpTransceiver::interruptDownload);
23
27
}
24
28
25
29
HttpTransceiver::~HttpTransceiver ()
26
30
{
27
31
delete ui;
28
32
}
29
33
30
- QByteArray HttpTransceiver::getDownloadedData ()
34
+ QTemporaryFile* HttpTransceiver::getDownloadedData ()
31
35
{
32
- return m_downloadData;
36
+ m_downloadFile->seek (0 );
37
+ return m_downloadFile;
33
38
}
34
39
35
40
void HttpTransceiver::setUploadMode (QByteArray data)
@@ -41,6 +46,7 @@ void HttpTransceiver::setUploadMode(QByteArray data)
41
46
ui->cb_verb ->setEnabled (true );
42
47
ui->le_formDataName ->setVisible (true );
43
48
ui->lb_formDataName ->setVisible (true );
49
+ ui->pr_request ->setValue (0 );
44
50
}
45
51
46
52
void HttpTransceiver::setDownloadMode ()
@@ -50,16 +56,20 @@ void HttpTransceiver::setDownloadMode()
50
56
ui->cb_verb ->setEnabled (false );
51
57
ui->le_formDataName ->setVisible (false );
52
58
ui->lb_formDataName ->setVisible (false );
59
+ ui->pr_request ->setValue (0 );
53
60
}
54
61
55
62
void HttpTransceiver::on_pb_send_clicked ()
56
63
{
57
64
ui->pb_send ->setEnabled (false );
65
+ ui->cb_verb ->setEnabled (false );
58
66
QNetworkRequest request (QUrl (ui->le_url ->text ()));
59
67
60
68
QNetworkReply *reply;
61
69
62
70
if (ui->cb_verb ->currentText () == " GET" ) {
71
+ ui->pb_interrupt ->setEnabled (true );
72
+ m_downloadFile->resize (0 );
63
73
reply = m_netManager->get (request);
64
74
65
75
connect (reply, &QNetworkReply::downloadProgress, this , &HttpTransceiver::progressReceived);
@@ -81,12 +91,23 @@ void HttpTransceiver::on_pb_send_clicked()
81
91
else if (ui->cb_verb ->currentText () == " PUT" ) {
82
92
reply = m_netManager->put (request, multiPart);
83
93
}
94
+ else {
95
+ QMessageBox msg (this );
96
+ msg.setWindowTitle (" Unsupported HTTP Verb" );
97
+ msg.setText (QString (" The selected HTTP Verb '%1' is unsupported" ).arg (ui->cb_verb ->currentText ()));
98
+ msg.setDefaultButton (QMessageBox::Ok);
99
+ msg.exec ();
100
+ return ;
101
+ }
84
102
85
103
multiPart->setParent (reply);
86
104
87
105
connect (reply, &QNetworkReply::uploadProgress, this , &HttpTransceiver::progressReceived);
88
106
}
89
107
108
+ if (m_reply) {
109
+ m_reply->deleteLater ();
110
+ }
90
111
m_reply = reply;
91
112
92
113
connect (
@@ -96,14 +117,24 @@ void HttpTransceiver::on_pb_send_clicked()
96
117
&HttpTransceiver::handleError);
97
118
}
98
119
120
+ void HttpTransceiver::interruptDownload ()
121
+ {
122
+ replyReceived (m_reply);
123
+ m_reply->abort ();
124
+ }
125
+
99
126
void HttpTransceiver::progressReceived (qint64 progress, qint64 total)
100
127
{
101
128
ui->pr_request ->setValue (int (double (progress) / double (total) * 100.0 ));
129
+ if (ui->cb_verb ->currentText () == " GET" ) {
130
+ while (m_reply->bytesAvailable ()) {
131
+ m_downloadFile->write (m_reply->read (10 * 1000 * 1000 ));
132
+ }
133
+ }
102
134
}
103
135
104
136
void HttpTransceiver::replyReceived (QNetworkReply *reply)
105
137
{
106
- m_downloadData = reply->read (1000 * 1000 * 10 );
107
138
m_uploadData.clear ();
108
139
109
140
if (reply->error () == QNetworkReply::NoError) {
@@ -113,16 +144,17 @@ void HttpTransceiver::replyReceived(QNetworkReply *reply)
113
144
ui->le_formDataName ->clear ();
114
145
}
115
146
116
- reply->deleteLater ();
117
147
ui->pb_send ->setEnabled (true );
148
+ ui->cb_verb ->setEnabled (true );
149
+ ui->pb_interrupt ->setEnabled (false );
118
150
}
119
151
120
152
void HttpTransceiver::handleError (QNetworkReply::NetworkError error)
121
153
{
122
154
Q_UNUSED (error)
123
155
QMessageBox msg (this );
124
156
msg.setWindowTitle (" HTTP Error" );
125
- msg.setText (QString (" Failed to perform HTTP request: '%1'" ).arg (m_reply->errorString ()));
157
+ msg.setText (QString (" Error encountered in HTTP request: '%1'" ).arg (m_reply->errorString ()));
126
158
msg.setDefaultButton (QMessageBox::Ok);
127
159
msg.exec ();
128
160
}
0 commit comments