Getting the HTTP standard response code from a QNetworkReply



  • I am working on some code to get an image from a web server using QNetworkReply. Since there is security to be concerned with, I need to look at the HTTP standard response code to determine if the result is a clean 200 or some type of error. How exactly do I get the the HTTP standard response code?



  • Have you tried "error()":http://doc.qt.nokia.com/4.7/qnetworkreply.html#error method?



  • The raw response code is available as an integer using the QNetworkReply::attribute(QNetworkRequest::HttpStatusCodeAttribute) method.



  • Connect a slot up to the QNetworkReply::metaDataChanged() signal. Then in that slot you can check what you need:

    @
    QVariant statusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
    if ( !statusCode.isValid() )
    return;

    int status = statusCode.toInt();
    
    if ( status != 200 )
    {
        QString reason = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString();
        qDebug() << reason;
    }
    

    @


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.