Results 1 to 2 of 2

Thread: qnetwork manager does not allow redirection?

  1. #1
    Join Date
    Dec 2007
    Location
    Groningen Netherlands
    Posts
    182
    Thanks
    16
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default qnetwork manager does not allow redirection?

    Hello,
    I made a very simple class to fetch a page as a string synchronous, but I saw that when there is a http redirect, qnetworkmanager just returns 'this page has moved'. Is there a solution for this?

    This is my class, please feel free to ciritize it. (except the use of upper/lowercase, I'm still getting used to / muttering about Qt's conventions)
    Qt Code:
    1. class Http : public QObject
    2. {
    3. Q_OBJECT
    4. QNetworkAccessManager *manager;
    5. private slots:
    6. void replyFinished(QNetworkReply *);
    7. public:
    8. bool ready, error;
    9. int timeout;
    10. QString Reply;
    11. Http();
    12. ~Http();
    13. };
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //-------------------------------------------------------------------------------
    2. Http::~Http()
    3. {
    4. delete manager;
    5. }
    6. //-------------------------------------------------------------------------------
    7. Http::Http()
    8. {
    9. manager = new QNetworkAccessManager(this);
    10. connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
    11. timeout = 60;
    12. Reply = "";
    13. }
    14. //-------------------------------------------------------------------------------
    15. void Http::replyFinished(QNetworkReply *reply)
    16. {
    17. error = reply->error();
    18. Reply = error ? "" : reply->readAll();
    19. ready = true;
    20. reply->deleteLater();
    21. }
    22. //-------------------------------------------------------------------------------
    23. QString Http::Get(QString S)
    24. {
    25. QUrl qrl(S);
    26. manager->get(QNetworkRequest(qrl));
    27. ready = false;
    28. QTime cur = QTime::currentTime().addSecs(timeout);
    29. while (!ready && QTime::currentTime() < cur)
    30. qApp->processEvents();
    31. return Reply;
    32. }
    To copy to clipboard, switch view to plain text mode 

    Use as:
    Qt Code:
    1. QCoreApplication a(argc, argv); // needed for qApp
    2. Http h;
    3. QString S = h.Get("http://someurl");
    To copy to clipboard, switch view to plain text mode 
    Last edited by JeanC; 17th March 2011 at 11:06.

  2. #2
    Join Date
    Dec 2010
    Location
    Israel
    Posts
    90
    Thanks
    59
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qnetwork manager does not allow redirection?

    Hi JeanC. I was dealing with the same issue right now.

    You can check the reply object to see if the status code is 301 (redirect). If so there's a redirection url attribute that would be set to the target URL.

    Qt Code:
    1. QVariant replyStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    2.  
    3. //! If needs to redirect - download from updated url
    4. if (replyStatus == 301)
    5. {
    6. QString redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toString();
    7.  
    8. // HERE - just use the url for creating a new request
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Connect internet for Qnetwork S60
    By Thành Viên Mới in forum Qt Programming
    Replies: 2
    Last Post: 5th June 2010, 03:41
  2. Tracking multiple requests with QNetwork
    By rbp in forum Qt Programming
    Replies: 4
    Last Post: 1st June 2010, 08:04
  3. Stdout and redirection in linux
    By themolecule in forum Qt Programming
    Replies: 6
    Last Post: 15th January 2008, 17:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.