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
| void QHttpDownloadObject::Init() { QNetworkAccessManager *m_pManager = new QNetworkAccessManager(this); m_pOnlinemanager = new QNetworkConfigurationManager(this);
qDebug() << m_pManager->supportedSchemes(); m_pManager->setNetworkAccessible(QNetworkAccessManager::Accessible);
QNetworkConfigurationManager manager; m_pManager->setConfiguration(manager.defaultConfiguration());
connect(m_pManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(slotRequestFinished(QNetworkReply*))); }
void QHttpDownloadObject::PerformRequest(QString strUrl) { QNetworkRequest request;
request.setUrl(QUrl(strUrl));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
m_fileTmp.setFileName(m_DestFilePath); m_fileTmp.open(QIODevice::Append);
m_pReply = m_pManager->get(request);
connect(m_pReply, &QNetworkReply::readyRead, this, &QHttpDownloadObject::onReadyRead); connect(m_pReply, &QNetworkReply::downloadProgress, this, &QHttpDownloadObject::downloadProgress); connect(m_pReply, static_cast<void(QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), this, &QHttpDownloadObject::onError);
}
void QHttpDownloadObject::slotRequestFinished(QNetworkReply *reply) { disconnect(m_pReply, SIGNAL(downloadProgress(qint64, qint64)), this, SIGNAL(downloadProgress(qint64, qint64)));
if (m_fileTmp.isOpen()) { m_fileTmp.close(); }
QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute); QString strError = reply->errorString(); QNetworkReply::NetworkError error = reply->error();
qDebug() << "+++++++++" << statusCode << strError << error;
if (statusCode.toInt() >= 400 || error != QNetworkReply::NoError) { QLOG_INFO() << "无法下载文件,错误状态码:" << statusCode << "错误信息:" << strError << error; emit sigDownloadFinished(DOWNLOAD_CODE_NETBAD, QString(ERRORTITLE_APPACCESS_NETFAULT).arg(strError)); } else { emit sigDownloadFinished(statusCode.toInt(), strError); } reply->close(); reply->deleteLater(); m_pManager->deleteLater(); m_pOnlinemanager->deleteLater(); }
|