Qt:QNetworkReply


0、說明

QNetworkReply對象包含了Manager發送的請求頭和返回的數據

它繼承自QIODevice,所以可以用各種read獲取其中返回的數據:

QByteArray data = reply->readAll();

QNetworkReply順序訪問QIODevice,這意味着一旦數據從中讀取出來之后,它就不再為Device保存了。因此保存數據的工作必須要由應用程序自己來實現。每當有新的數據從網絡中傳來時,readyRead()信號就會被發送。

此外,數據接收時downloadProgress()信號也會被發送,只是其中包含的bytes數並不代表實際接收的bytes,因為可能存在着數據轉換(如解壓縮和撤銷協議)。

它也會發送uploadProgress()信號,指示上傳這些內容的進度。

 

注意

不要在信號errorOccurred()finished()的槽中刪除Object,而應該使用deleteLater()

1、模塊和加載項

Header: #include <QNetworkReply>
qmake: QT += network
Since: Qt 4.4
Inherits: QIODevice

2、構造

3、靜態字段

類型

字段

說明

enum NetworkError

標識在處理Request期間可能發生的所有錯誤。

常見的是QNetworkReply::NoError,標識沒有錯誤。

typedef RawHeaderPair 一個QPair < QByteArray , QByteArray >,第一個QByteArray是header name,第二個QByteArray是header

NetworkError

Constant Value Description
QNetworkReply::ConnectionRefusedError 1 the remote server refused the connection (the server is not accepting requests)
QNetworkReply::RemoteHostClosedError 2 the remote server closed the connection prematurely, before the entire reply was received and processed
QNetworkReply::HostNotFoundError 3 the remote host name was not found (invalid hostname)
QNetworkReply::TimeoutError 4 the connection to the remote server timed out
QNetworkReply::OperationCanceledError 5 the operation was canceled via calls to abort() or close() before it was finished.
QNetworkReply::SslHandshakeFailedError 6 the SSL/TLS handshake failed and the encrypted channel could not be established. The sslErrors() signal should have been emitted.
QNetworkReply::TemporaryNetworkFailureError 7 the connection was broken due to disconnection from the network, however the system has initiated roaming to another access point. The request should be resubmitted and will be processed as soon as the connection is re-established.
QNetworkReply::NetworkSessionFailedError 8 the connection was broken due to disconnection from the network or failure to start the network.
QNetworkReply::BackgroundRequestNotAllowedError 9 the background request is not currently allowed due to platform policy.
QNetworkReply::TooManyRedirectsError 10 while following redirects, the maximum limit was reached. The limit is by default set to 50 or as set by QNetworkRequest::setMaxRedirectsAllowed(). (This value was introduced in 5.6.)
QNetworkReply::InsecureRedirectError 11 while following redirects, the network access API detected a redirect from a encrypted protocol (https) to an unencrypted one (http). (This value was introduced in 5.6.)
QNetworkReply::ProxyConnectionRefusedError 101 the connection to the proxy server was refused (the proxy server is not accepting requests)
QNetworkReply::ProxyConnectionClosedError 102 the proxy server closed the connection prematurely, before the entire reply was received and processed
QNetworkReply::ProxyNotFoundError 103 the proxy host name was not found (invalid proxy hostname)
QNetworkReply::ProxyTimeoutError 104 the connection to the proxy timed out or the proxy did not reply in time to the request sent
QNetworkReply::ProxyAuthenticationRequiredError 105 the proxy requires authentication in order to honour the request but did not accept any credentials offered (if any)
QNetworkReply::ContentAccessDenied 201 the access to the remote content was denied (similar to HTTP error 403)
QNetworkReply::ContentOperationNotPermittedError 202 the operation requested on the remote content is not permitted
QNetworkReply::ContentNotFoundError 203 the remote content was not found at the server (similar to HTTP error 404)
QNetworkReply::AuthenticationRequiredError 204 the remote server requires authentication to serve the content but the credentials provided were not accepted (if any)
QNetworkReply::ContentReSendError 205 the request needed to be sent again, but this failed for example because the upload data could not be read a second time.
QNetworkReply::ContentConflictError 206 the request could not be completed due to a conflict with the current state of the resource.
QNetworkReply::ContentGoneError 207 the requested resource is no longer available at the server.
QNetworkReply::InternalServerError 401 the server encountered an unexpected condition which prevented it from fulfilling the request.
QNetworkReply::OperationNotImplementedError 402 the server does not support the functionality required to fulfill the request.
QNetworkReply::ServiceUnavailableError 403 the server is unable to handle the request at this time.
QNetworkReply::ProtocolUnknownError 301 the Network Access API cannot honor the request because the protocol is not known
QNetworkReply::ProtocolInvalidOperationError 302 the requested operation is invalid for this protocol
QNetworkReply::UnknownNetworkError 99 an unknown network-related error was detected
QNetworkReply::UnknownProxyError 199 an unknown proxy-related error was detected
QNetworkReply::UnknownContentError 299 an unknown error related to the remote content was detected
QNetworkReply::ProtocolFailure 399 a breakdown in protocol was detected (parsing error, invalid or unexpected responses, etc.)
QNetworkReply::UnknownServerError 499 an unknown error related to the server response was detected

 

4、實例方法

返回值類型

方法

說明

QVariant attribute(QNetworkRequest::Attribute code)  
QNetworkReply::NetworkError error()  
bool hasRawHeader(QByteArray headerName)  
QVariant header(QNetworkRequest::KnownHeaders header)  
void ignoreSslErrors(QList<QSslError> errors)  
bool isFinished()  
bool isRunning()  
QNetworkAccessManager * manager()  
QNetworkAccessManager::Operation operation()  
QByteArray rawHeader(QByteArray headerName)  
QList<QByteArray> rawHeaderList()  
QList<QNetworkReply::RawHeaderPair> rawHeaderPairs()  
qint64 readBufferSize()  
QNetworkRequest request()  
virtual void setReadBufferSize(qint64 size)  
void setSslConfiguration(QSslConfiguration config)  
QSslConfiguration sslConfiguration()  
QUrl url()  

5、信號

信號

說明

downloadProgress(qint64 bytesReceived, qint64 bytesTotal)  
encrypted()  
errorOccurred(QNetworkReply::NetworkError code)  
finished()  
metaDataChanged()  
preSharedKeyAuthenticationRequired(QSslPreSharedKeyAuthenticator *authenticator)  
redirectAllowed()  
redirected(QUrl url)  
sslErrors(QList<QSslError> errors)  
uploadProgress(qint64 bytesSent, qint64 bytesTotal)  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM