Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QNetworkRequest Redirect doesn't seem to work in Qt6
QtWS25 Last Chance

QNetworkRequest Redirect doesn't seem to work in Qt6

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 3 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hbatalha
    wrote on last edited by hbatalha
    #1

    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)

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      H 3 Replies Last reply
      0
      • SGaistS SGaist

        Hi,

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

        H Offline
        H Offline
        hbatalha
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

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

          H Offline
          H Offline
          hbatalha
          wrote on last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

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

            H Offline
            H Offline
            hbatalha
            wrote on last edited by
            #5

            @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

            eyllanescE 1 Reply Last reply
            0
            • H hbatalha

              @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

              eyllanescE Offline
              eyllanescE Offline
              eyllanesc
              wrote on last edited by
              #6

              @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
              

              If you want me to help you develop some work then you can write to my email: e.yllanescucho@gmal.com.

              H 1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                So it's all good ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • eyllanescE eyllanesc

                  @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
                  
                  H Offline
                  H Offline
                  hbatalha
                  wrote on last edited by
                  #8

                  @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)
                      });
                  
                  H 1 Reply Last reply
                  0
                  • H hbatalha

                    @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)
                        });
                    
                    H Offline
                    H Offline
                    hbatalha
                    wrote on last edited by
                    #9

                    @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.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved