Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QNetworkRequest Redirect doesn't seem to work in Qt6



  • In Qt5 this works great:

    auto manager = new QNetworkAccessManager();
    
    QObject::connect(manager, &QNetworkAccessManager::finished,
            [&](QNetworkReply* repl) {
        qDebug() << repl->readAll();
    });
    QNetworkRequest req(QUrl("http://www.dropbox.com/s/ehoawgm89yz6pib/version.txt?raw=1")); 
    req.setAttribute(QNetworkRequest::FollowRedirectsAttribute, true);
    req.setMaximumRedirectsAllowed(3);
    QNetworkReply* reply = manager->get(req);
    

    But Qt6 brought changes to Qt Network. In the Redirect policies it states:

    In Qt 6, the default redirect policy has changed from manual to QNetworkRequest::NoLessSafeRedirectPolicy. If your application relies on manual redirect handling (it connects its slot to the QNetworkReply::redirected signal), you have to explicitly set this policy when creating a request:

    request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::ManualRedirectPolicy);
    

    In the doc it says Network Access API does follow redirections by default, but it doesn't work. This what I have tried:

    QNetworkRequest req(QUrl("http://www.dropbox.com/s/ehoawgm89yz6pib/version.txt?raw=1"));
    req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
    eq.setMaximumRedirectsAllowed(3);
    

    Also when using Qt5 it warns that QNetworkRequest::FollowRedirectsAttribute is deprecated and that I should use QNetworkRequest::RedirectPolicyAttribute(this compiles in Qt6 but doesn't download anything, it works in Qt5)


  • Lifetime Qt Champion

    Hi,

    Did you connect the various error signals to see what happens on that front ?



  • @SGaist Nope, I will do that and get back to you



  • This post is deleted!


  • @SGaist @SGaist With this code:

    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QNetworkAccessManager* net = new QNetworkAccessManager;
    
        auto reply = net->get(QNetworkRequest(QUrl("https://www.dropbox.com/s/zqbxgw4owoteas0/version.txt?dl=1")));
    
        QObject::connect(reply, &QNetworkReply::downloadProgress, [](qint64 received, qint64 total)
        {
            qDebug() << received << " of " << total;
        });
    
        QObject::connect(net, &QNetworkAccessManager::finished, [](QNetworkReply* reply)
        {
            if(! reply->error())
            {
                QByteArray data = reply->readAll();
    
                QString dest_file = "D:/Desktop/test.txt";
    
                QFile file(dest_file);
    
                if(QFile::exists(dest_file))
                    QFile::remove(dest_file);
    
                if( ! file.open(QIODevice::ReadWrite | QIODevice::Truncate))
                    qDebug () << "Could not save the file";
    
                file.write(data);
                file.close();
            }
            else
                qDebug() << "ERROR: " << reply->errorString();
    
            reply->deleteLater();
        });
    
        return a.exec();
    }
    

    I get the following error:

    ERROR: "Host not found"

    I will g search to see what comes up



  • @hbatalha I just tested your code adding request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::ManualRedirectPolicy); and it works fine.

    #include <QCoreApplication>
    #include <QFile>
    #include <QNetworkAccessManager>
    #include <QNetworkReply>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        QNetworkAccessManager net;
    
        QNetworkRequest request(QUrl("https://www.dropbox.com/s/zqbxgw4owoteas0/version.txt?dl=1"));
        request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::ManualRedirectPolicy);
    
        QNetworkReply *reply = net.get(request);
    
        QObject::connect(reply, &QNetworkReply::downloadProgress, [](qint64 received, qint64 total)
        {
            qDebug() << received << " of " << total;
        });
    
        QObject::connect(&net, &QNetworkAccessManager::finished, [](QNetworkReply* reply)
        {
            if(! reply->error())
            {
                QByteArray data = reply->readAll();
    
                QString dest_file = "test.txt";
    
                QFile file(dest_file);
    
                if(QFile::exists(dest_file))
                    QFile::remove(dest_file);
    
                if( ! file.open(QIODevice::ReadWrite | QIODevice::Truncate))
                    qDebug () << "Could not save the file";
    
                file.write(data);
                file.close();
            }
            else
                qDebug() << "ERROR: " << reply->errorString();
    
            reply->deleteLater();
        });
    
        return a.exec();
    }
    

    Output

    135  of  -1
    135  of  135
    

  • Lifetime Qt Champion

    So it's all good ?



  • @eyllanesc From the OP :
    But Qt6 brought changes to Qt Network. In the Redirect policies it states:

    In Qt 6, the default redirect policy has changed from manual to QNetworkRequest::NoLessSafeRedirectPolicy. If your application relies on manual redirect handling (it connects its slot to the QNetworkReply::redirected signal), you have to explicitly set this policy when creating a request:

    request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::ManualRedirectPolicy);
    

    With that option and without a manual redirect handling what it does is write the text below to file:

    301 Moved Permanently

    The resource has been moved to /s/dl/zqbxgw4owoteas0/version.txt;

    you should be redirected automatically.

    I don't want to to create a slot to handle redirect as it should work without but I tried either way and the signal for redirect doesn't to be emitted or I am writing the code wrong:

    req.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::ManualRedirectPolicy);
    
        QNetworkReply* reply;
        reply = net->get(req);
    
        QObject::connect(reply, &QNetworkReply::redirected, [] (QUrl url)
        {
            qDebug() << "HERE"; // never called
            
            redirect(url); //void redirect(QUrl url)
        });
    


  • @hbatalha Update:

    I've upgraded Qt to version 6.1.2 MinGW 64-bit. The redirect seems to be working as I was able to download GDrive direct link that had redirect but I can't download a dropbox file, I have tested the link with pure c curl and it works great but with QNetworkAccessManager I keep getting the 'Host not found' error though.

    I am looking for more links with redirects to tests. I update any news here.


Log in to reply