c++qtqt5qnetworkreplyqnetworkrequest

Follow redirects in QT5.5


I have a program that works well from 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);

Unfortunately this only works from Qt 5.6

Could anyone help me to make the redirect o Qt5.5 (Ubuntu 16.04) I guess I have to follow the redirection manually, but I have found no tutorial on it.

I found a solution for Qt4 -> QNetworkReply and 301 redirect

I was hoping there is something more "updated" for Qt5.

Thanks


Solution

  • Here it is the way I have solved it following Qt4 style as in QNetworkReply and 301 redirect.

    If anyone has a better solution please 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());