0

我有一个从 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 和 301 重定向的解决方案

我希望 Qt5 有更多“更新”的东西。

谢谢

4

1 回答 1

0

这是我按照QNetworkReply 和 301 redirect中的 Qt4 样式解决它的方法。

如果有人有更好的解决方案,请发布

    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());
于 2018-07-17T17:00:54.353 回答