在 QT5.5 中跟随重定向

Follow redirects in QT5.5

我有一个在 Qt 5.6 上运行良好的程序

const QUrl qurl(url);
QNetworkRequest request(qurl);
//github redirects the request, so this attribute must be set to true, otherwise returns nothing
//from qt5.6
request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
QNetworkAccessManager manager;
QNetworkReply * reply = manager.get(request);

不幸的是,这仅适用于 Qt 5.6

任何人都可以帮助我对 Qt5.5 (Ubuntu 16.04) 进行重定向,我想我必须手动进行重定向,但我没有找到相关教程。

我找到了 Qt4 的解决方案 -> QNetworkReply and 301 redirect

我希望 Qt5 有更多 "updated"。

谢谢

这是我按照 QNetworkReply and 301 redirect 中的 Qt4 风格解决它的方法。

如果有人有更好的解决方案请post

    QNetworkAccessManager manager;
    QVariant possibleRedirectUrl;
    QNetworkReply * reply;

    QUrl qurl;
    qurl.setUrl(url);

    //we check if there is any redirect to follow it
    //from Qt 5.6 redirects can be automatically followed by setting
    //request.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
    //here is done automatically becaus of Ubuntu 16.04 Qt version (5.5)

    do{
        qInfo() <<"Downloading " << qurl;

        QNetworkRequest request(qurl);
        reply = manager.get(request);

        qInfo() << "waiting to finish...";

        _timeout=false;
        _timer->start(timeout);

        //if download takes more time that set on timeout cancel download
        while(!_timeout){
            qApp->processEvents();
            if(reply->isFinished()) break;
        }

        _timeout = false;

        qInfo() << "finished!";

        possibleRedirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
        qurl = possibleRedirectUrl.toUrl();
        qInfo() << qurl;

    }while(possibleRedirectUrl.isValid());