Skip to content

Commit 738de74

Browse files
author
wangwj-f
committed
新增strError返回字段,与byteContent区分开
1 parent 37bc984 commit 738de74

10 files changed

+55
-31
lines changed

include/networkdef.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ struct RequestTask
9292

9393
// 请求是否成功
9494
bool bSuccess;
95-
// 请求返回的内容/返回的错误信息等.
95+
// 请求返回的内容
9696
QByteArray bytesContent;
97+
// 返回的错误信息
98+
QString strError;
9799

98100
// 请求ID
99101
quint64 uiId;

samples/networktool/networktool.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ void NetworkTool::onBatchRequest()
874874
void NetworkTool::onRequestFinished(const RequestTask &request)
875875
{
876876
bool bBatch = (request.uiBatchId > 0);
877-
appendTaskFinishMsg(request.uiId, bBatch, request.bSuccess, request.url.url(), request.bytesContent);
877+
appendTaskFinishMsg(request.uiId, bBatch, request.bSuccess, request.url.url(), request.bytesContent, request.strError);
878878

879879
if (bBatch)
880880
{
@@ -1031,7 +1031,7 @@ void NetworkTool::appendStartBatchTasksMsg(quint64 uiBatchid, int nTotalSize)
10311031
appendMsg(QTime::currentTime().toString() + str);
10321032
}
10331033

1034-
void NetworkTool::appendTaskFinishMsg(quint64 uiTaskid, bool isBatch, bool bSuccess, const QString& strUrl, const QString& strBody)
1034+
void NetworkTool::appendTaskFinishMsg(quint64 uiTaskid, bool isBatch, bool bSuccess, const QString& strUrl, const QString& strBody, const QString& strError)
10351035
{
10361036
if (RequestTask::ALL_TASK == uiTaskid)
10371037
{
@@ -1056,6 +1056,10 @@ void NetworkTool::appendTaskFinishMsg(quint64 uiTaskid, bool isBatch, bool bSucc
10561056
{
10571057
appendMsg(QString("[Body][id:%1]\n").arg(uiTaskid) + strBody, !isBatch);
10581058
}
1059+
if (!strError.isEmpty())
1060+
{
1061+
appendMsg(QString("[Error][id:%1]\n").arg(uiTaskid) + strError, !isBatch);
1062+
}
10591063
}
10601064

10611065
QString NetworkTool::getDefaultDownloadDir()

samples/networktool/networktool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private Q_SLOTS:
5959
void appendMsg(const QString& strMsg, bool bQDebug = true);
6060
void appendStartTaskMsg(quint64 uiTaskid, const QString& strUrl);
6161
void appendStartBatchTasksMsg(quint64 uiBatchid, int nTotalSize);
62-
void appendTaskFinishMsg(quint64 uiTaskid, bool isBatch, bool bSuccess, const QString& strUrl, const QString& strBody);
62+
void appendTaskFinishMsg(quint64 uiTaskid, bool isBatch, bool bSuccess, const QString& strUrl, const QString& strBody, const QString& strError);
6363

6464
void switchTaskView(bool bForceDoing = false);
6565
void reset();

src/inc/networkdef.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,10 @@ struct RequestTask
9292

9393
// 请求是否成功
9494
bool bSuccess;
95-
// 请求返回的内容/返回的错误信息等.
95+
// 请求返回的内容
9696
QByteArray bytesContent;
97+
// 返回的错误信息
98+
QString strError;
9799

98100
// 请求ID
99101
quint64 uiId;

src/networkcommonrequest.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void NetworkCommonRequest::start()
3737
m_strError = QStringLiteral("Unsupported FTP request type[%1], url: %2").arg(strType).arg(m_request.url.url());
3838
qDebug() << "[QMultiThreadNetwork]" << m_strError;
3939
LOG_ERROR(m_strError.toStdWString());
40-
emit requestFinished(false, m_strError.toUtf8());
40+
emit requestFinished(false, QByteArray(), m_strError);
4141
return;
4242
}
4343
}
@@ -141,6 +141,7 @@ void NetworkCommonRequest::onFinished()
141141
}
142142
}
143143

144+
QByteArray bytes;
144145
if (!m_bAbortManual)//非调用abort()结束
145146
{
146147
if (bSuccess && m_request.eType == eTypeHead)
@@ -152,25 +153,24 @@ void NetworkCommonRequest::onFinished()
152153
.arg(QString::fromUtf8(m_pNetworkReply->rawHeader(header))));
153154
headers.append("\n");
154155
}
155-
emit requestFinished(bSuccess, headers.toUtf8());
156+
bytes = headers.toUtf8();
156157
}
157158
else
158159
{
159160
if (m_pNetworkReply->isOpen())
160161
{
161-
const QByteArray& bytes = m_pNetworkReply->readAll();
162-
if (!bytes.isEmpty())
162+
if (bSuccess)
163163
{
164-
m_strError.append(QString::fromUtf8(bytes));
164+
bytes = m_pNetworkReply->readAll();
165+
}
166+
else
167+
{
168+
m_strError.append(QString::fromUtf8(m_pNetworkReply->readAll()));
165169
}
166170
}
167-
emit requestFinished(bSuccess, m_strError.toUtf8());
168171
}
169172
}
170-
else//调用abort()结束
171-
{
172-
emit requestFinished(bSuccess, m_strError.toUtf8());
173-
}
173+
emit requestFinished(bSuccess, bytes, m_strError);
174174

175175
m_pNetworkReply->deleteLater();
176176
m_pNetworkManager->deleteLater();

src/networkdownloadrequest.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ void NetworkDownloadRequest::start()
179179
}
180180
else
181181
{
182-
emit requestFinished(false, m_strError.toUtf8());
182+
emit requestFinished(false, QByteArray(), m_strError);
183183
}
184184
}
185185

@@ -265,7 +265,17 @@ void NetworkDownloadRequest::onFinished()
265265
}
266266
}
267267

268-
emit requestFinished(bSuccess, m_strError.toUtf8());
268+
if (!m_bAbortManual)//非调用abort()结束
269+
{
270+
if (m_pNetworkReply->isOpen())
271+
{
272+
if (!bSuccess)
273+
{
274+
m_strError.append(QString::fromUtf8(m_pNetworkReply->readAll()));
275+
}
276+
}
277+
}
278+
emit requestFinished(bSuccess, QByteArray(), m_strError);
269279

270280
m_pNetworkReply->deleteLater();
271281
m_pNetworkReply = nullptr;

src/networkmtdownloadrequest.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void NetworkMTDownloadRequest::start()
194194
if (!b)
195195
{
196196
m_strError = QStringLiteral("Invalid Url").toUtf8();
197-
emit requestFinished(false, m_strError.toUtf8());
197+
emit requestFinished(false, QByteArray(), m_strError);
198198
}
199199
}
200200

@@ -211,7 +211,7 @@ void NetworkMTDownloadRequest::startMTDownload()
211211
m_strError = QStringLiteral("MT download 服务器未返回Content-Length");
212212
qDebug() << "[QMultiThreadNetwork]" << m_strError;
213213
LOG_INFO(m_strError.toStdWString());
214-
emit requestFinished(false, m_strError.toUtf8());
214+
emit requestFinished(false, QByteArray(), m_strError);
215215
return;
216216
}
217217

@@ -262,14 +262,14 @@ void NetworkMTDownloadRequest::startMTDownload()
262262
abort();
263263
m_strError = QStringLiteral("Subpart %1 startDownload() failed!").arg(i);
264264
LOG_ERROR(m_strError.toStdWString());
265-
emit requestFinished(false, m_strError.toUtf8());
265+
emit requestFinished(false, QByteArray(), m_strError);
266266
return;
267267
}
268268
}
269269
}
270270
else
271271
{
272-
emit requestFinished(false, m_strError.toUtf8());
272+
emit requestFinished(false, QByteArray(), m_strError);
273273
}
274274
}
275275

@@ -300,7 +300,7 @@ void NetworkMTDownloadRequest::onSubPartFinished(int index, bool bSuccess, const
300300
//如果完成数等于文件段数,则说明文件下载成功;失败数大于0,说明下载失败
301301
if (m_nSuccess == m_nThreadCount || m_nFailed == 1)
302302
{
303-
emit requestFinished((m_nFailed == 0), m_strError.toUtf8());
303+
emit requestFinished((m_nFailed == 0), QByteArray(), m_strError);
304304
qDebug() << "[QMultiThreadNetwork] MT download finished. [result]" << (m_nFailed == 0);
305305
}
306306
}
@@ -381,7 +381,7 @@ void NetworkMTDownloadRequest::onFinished()
381381
m_strError = QStringLiteral("MT download get file size failed! http status code(%1)").arg(statusCode);
382382
qDebug() << "[QMultiThreadNetwork]" << m_strError;
383383
LOG_ERROR(m_strError.toStdWString());
384-
emit requestFinished(false, m_strError.toUtf8());
384+
emit requestFinished(false, QByteArray(), m_strError);
385385
return;
386386
}
387387
}

src/networkrequest.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public Q_SLOTS:
3131
virtual void onAuthenticationRequired(QNetworkReply *, QAuthenticator *);
3232

3333
Q_SIGNALS:
34-
void requestFinished(bool, const QByteArray&);
34+
void requestFinished(bool bSuccess, const QByteArray& strContent, const QString& strError);
3535
void aboutToAbort();
3636

3737
protected:

src/networkrunnable.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void NetworkRunnable::run()
3131

3232
try
3333
{
34-
connect(this, &NetworkRunnable::exitEventLoop, &loop, [&bQuit, &loop, &pRequest, &task]() {
34+
connect(this, &NetworkRunnable::exitEventLoop, &loop, [&bQuit, &loop, &pRequest]() {
3535
loop.quit();
3636
bQuit = true;
3737
if (pRequest.get())
@@ -47,9 +47,10 @@ void NetworkRunnable::run()
4747
if (pRequest.get())
4848
{
4949
connect(pRequest.get(), &NetworkRequest::requestFinished,
50-
[this, &task](bool bSuccess, const QByteArray& bytesContent) {
50+
[this, &task](bool bSuccess, const QByteArray& bytesContent, const QString& strError) {
5151
task.bSuccess = bSuccess;
5252
task.bytesContent = bytesContent;
53+
task.strError = strError;
5354
emit requestFinished(task);
5455
});
5556
pRequest->setRequestTask(task);
@@ -61,6 +62,7 @@ void NetworkRunnable::run()
6162
LOG_ERROR("Unsupported type(" << task.eType << ") ---- " << task.url.url().toStdWString());
6263

6364
task.bSuccess = false;
65+
task.strError = QString("Unsupported type(%1)").arg(task.eType);
6466
emit requestFinished(task);
6567
}
6668
loop.exec();

src/networkuploadrequest.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void NetworkUploadRequest::start()
9696
}
9797
else
9898
{
99-
emit requestFinished(false, m_strError.toUtf8());
99+
emit requestFinished(false, QByteArray(), m_strError);
100100
}
101101
}
102102

@@ -140,18 +140,22 @@ void NetworkUploadRequest::onFinished()
140140
}
141141
}
142142

143+
QByteArray bytes;
143144
if (!m_bAbortManual)//非调用abort()结束
144145
{
145146
if (m_pNetworkReply->isOpen())
146147
{
147-
const QByteArray& bytes = m_pNetworkReply->readAll();
148-
if (!bytes.isEmpty())
148+
if (bSuccess)
149+
{
150+
bytes = m_pNetworkReply->readAll();
151+
}
152+
else
149153
{
150-
m_strError.append(QString::fromUtf8(bytes));
154+
m_strError.append(QString::fromUtf8(m_pNetworkReply->readAll()));
151155
}
152156
}
153157
}
154-
emit requestFinished(bSuccess, m_strError.toUtf8());
158+
emit requestFinished(bSuccess, bytes, m_strError);
155159

156160
m_pNetworkReply->deleteLater();
157161
m_pNetworkReply = nullptr;

0 commit comments

Comments
 (0)