1. 程式人生 > 其它 >Qt 網路程式設計之HTTP通訊(QNetworkRequest、QNetworkReply、NetworkAccessManager)

Qt 網路程式設計之HTTP通訊(QNetworkRequest、QNetworkReply、NetworkAccessManager)

技術標籤:QTqt

簡介

  • Qt網路模組提供了一些類實現OSI 7層網路模型中高層的網路協議,如HTTP、FTP、SNMP等
    這些類主要為:
QNetworkRequest類:
  • 通過一個URL地址發起網路協議請求,也儲存網路請求的資訊,目前支援HTTP、FTP和區域性檔案URLs的下載或上傳

The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
QNetworkRequest is part of the Network Access API and is the class holding the information necessary to send a request over the network. It contains a URL and some ancillary information that can be used to modify the request.

NetworkAccessManager類:

用於協調網路操作。在QNetworkRequest發起一個網路請求後,NetworkAccessManager負責傳送網路請求,建立網路響應

QNetworkReply類:
  • 表示網路請求的響應,包含了響應的資料。

The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
The QNetworkReply class contains the data and meta data related to a request posted with QNetworkAccessManager. Like QNetworkRequest, it contains a URL and headers (both in parsed and raw form), some information about the reply’s state and the contents of the reply itself.

QNetworkReply is a sequential-access QIODevice, which means that once data is read from the object, it no longer kept by the device. It is therefore the application’s responsibility to keep this data if it needs to. Whenever more data is received from the network and processed, the readyRead() signal is emitted.
The downloadProgress() signal is also emitted when data is received, but the number of bytes contained in it may not represent the actual bytes received, if any transformation is done to the contents (for example, decompressing and removing the protocol overhead).
Even though QNetworkReply is a QIODevice connected to the contents of the reply, it also emits the uploadProgress() signal, which indicates the progress of the upload for operations that have such content.
Note: Do not delete the object in the slot connected to the error() or finished() signal. Use deleteLater().

  • 由NetworkAccessManager在傳送一個網路請求後建立一個網路響應。QNetworkReply提供finished()、readyRead()、downloadProgress()可以監測網路響應的執行情況,執行相應的操作。
  • readyRead訊號:有新資料到來,可以從device進行讀取的時候會被髮送這個訊號。
  • 在error() 或 finished() 訊號對應的槽函式中不能delete QNetworkReply物件,要使用deleteLater()來刪除
  • QNetworkReply是QIODevice的子類,所以QNetworkReply支援流讀寫功能,也支援非同步或同步工作

例子

QUrl qurl;
QString sUrl;
qurl.setUrl(sUrl, QUrl::TolerantMode);
QNetworkRequest request;
request.setUrl(qurl);
//傳送網路請求,建立網路響應
QNetworkReply *reply=networkManager->get(request);
//關聯訊號與槽
connect(reply,SIGNAL(finished()),this,SLOT(on_finished()));
connect(reply,SIGNAL(readyRead()),this,SLOT(on_readyRead()));
connect(reply,SIGNAL(downloadProgress(qint64,qint64)),
            this,SLOT(on_downloadProgress(qint64,qint64)));

//讀取下載的資料
on_readyRead()
{
downloadedFile->write(reply->readAll());
}
//更新進度
on_downloadProgress(qint64 bytesRead,qint64 totalBytes)
{    
    ui->progressBar->setMaximum(totalBytes);
    ui->progressBar->setValue(bytesRead);
}
//請求結束之後
on_finished()
{
    QFileInfo fileInfo;
    fileInfo.setFile(downloadedFile->fileName());
    
    //關閉開啟的網路檔案,然後置空
    downloadedFile->close();
    delete downloadedFile;
    downloadedFile=Q_NULLPTR;
    
    //刪除網路響應並置空
    reply->deleteLater();
    reply=Q_NULLPTR;
    
    QMessageBox::information(this,QStringLiteral("提示"),QStringLiteral("下載成功"));
 
    //如果設定了下載完成後開啟,就開啟檔案
    if(ui->checkOpen->isChecked())
    {
        //呼叫預設的應用程式軟體開啟下載的檔案
        QDesktopServices::openUrl(QUrl::fromLocalFile(fileInfo.absoluteFilePath()));
        ui->btnDownload->setEnabled(true);
    }
}

部分內容參考自:http://www.skcircle.com/?id=972