Time for action – displaying a proper error message
Fortunately, QNetworkReply
offers several possibilities to do this. In the slot called downloadFinished()
, we first want to check whether an error occurred:
if (reply->error() != QNetworkReply::NoError) {/* error occurred */}
The QNetworkReply::error()
function returns the error that occurred while handling the request. The error is encoded as a value of the QNetworkReply::NetworkError
type. The two most common errors are probably these:
Error code |
Meaning |
---|---|
|
This error indicates that the URL of the request could not be found. It is similar to the HTTP error code 404. |
|
This error indicates that you do not have the permission to access the requested file. It is similar to the HTTP error code 401. |
You can look up the other 23 error codes in the documentation. But normally, you do not need to know exactly what went wrong. You only need to know whether everything worked out—QNetworkReply::NoError...