QtWebkit: How to check HTTP status code?

后端 未结 2 499
有刺的猬
有刺的猬 2021-01-02 05:33

I\'m writing a thumbnail generator as per an example in the QtWebkit documentation. I would like to avoid screenshots of error pages such as 404 not found or

2条回答
  •  迷失自我
    2021-01-02 05:39

    This is what I'm using in a porting project. It checks the reply and decides to start backing off making request or not. The backing off part is in progress but I left the comments in.

    QNetworkReply* reply = qobject_cast(sender());
    Q_CHECK_PTR(reply);
    
    QVariant statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    if (!statusCode.isNull() && statusCode.toInt() >= 400){
        //INVALID_SERVER_RESPONSE_BACKOFF;
        qDebug() << "server returned invalid response." << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
        return;
    }else if (!statusCode.isNull() && statusCode.toInt() != 200){
        //INVALID_SERVER_RESPONSE_NOBACKOFF;
        qDebug() << "server returned invalid response." << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
        return;
    }
    

提交回复
热议问题