Results 1 to 6 of 6

Thread: QNetworkAccessManager and http redirection

  1. #1
    Join Date
    May 2011
    Posts
    46
    Thanks
    5

    Default QNetworkAccessManager and http redirection

    Is it true that QNetworkAccessManager has no built-in support for http redirection?
    I would like to download raw html from the Internet but it seems that I can't get to the final redirected webpage.

    Qt Code:
    1. #ifndef PROGRAM_H
    2. #define PROGRAM_H
    3.  
    4. #include <QtNetwork>
    5. #include <QtCore>
    6.  
    7. class Program : public QObject
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. Program()
    13. {
    14. net = 0;
    15. }
    16. ~Program()
    17. {
    18. delete net;
    19. }
    20. void run()
    21. {
    22. net = new QNetworkAccessManager;
    23. connect(net, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
    24. net->get(QNetworkRequest(QUrl("http://www.yahoo.com")));
    25. }
    26.  
    27. private slots:
    28. void replyFinished(QNetworkReply* r){
    29. QString s = r->readAll();
    30. qDebug() << s;
    31. }
    32. private :
    33. QNetworkAccessManager* net;
    34. };
    35.  
    36. #endif // PROGRAM_H
    To copy to clipboard, switch view to plain text mode 

    When run it called, I got the following:
    Qt Code:
    1. <!-- w84.fp.sk1.yahoo.com uncompressed/chunked Thu Jul 7 08:37:01 PDT 2011 -->
    To copy to clipboard, switch view to plain text mode 
    instead of the final redirected webpage.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QNetworkAccessManager and http redirection

    What you are getting is the last line only. I get much more:

    --- snip! ---
    if ( "undefined" != typeof(rt_LogTime) ) {
    rt_LogTime(
    "t1", rtJSLoad - rtJSStart, true);
    rt_LogTime("t2", rtJSDone - rtJSLoad
    , true);
    if ("undefined" != typeof(rtAdStart) && "undefined" != typeof(rt
    AdDone)) { rt_LogTime("t3", rtAdDone - rtAdStart, true); }
    }
    </script></body>

    </html>
    <!-- bid=701 -->
    <!-- sid=2023538075 -->
    2c

    <!-- myproperty:myservice-us:0:Success -->

    50
    <!-- w25.fp.ch1.yahoo.com uncompressed/chunked Thu Jul 7 14:33:30 PDT 2011 -->

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QNetworkAccessManager and http redirection

    What greyfox is getting is the body content of the initial server response. It is not following the redirect, which you will find in the Location: header of the response.

    In the finished() signal you could check the QNetworkRequest::RedirectionTargetAttribute attribute and, if the return is valid, create a new request and get() it. You might want to test for redirect loops or endless redirects though.

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QNetworkAccessManager and http redirection

    Although I didn't follow any redirect requests, and to confirm it I also got the above by using telnet to connect to www.yahoo.com, port 80.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QNetworkAccessManager and http redirection

    I get a full response through telnet with a bare request also. However, using wget or QNetworkAccessManager the "Host:" header is sent and that triggers a different response from Yahoo.
    Qt Code:
    1. chrisw@newton /tmp $ telnet www.yahoo.com 80
    2. Trying 98.137.149.56...
    3. Connected to any-fp.wa1.b.yahoo.com.
    4. Escape character is '^]'.
    5. GET / HTTP/1.0
    6. Host: www.yahoo.com
    7.  
    8. HTTP/1.0 302 Found
    9. Date: Fri, 08 Jul 2011 07:41:09 GMT
    10. P3P: policyref="http://info.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC GOV"
    11. Cache-Control: private
    12. Set-Cookie: IU=deleted; expires=Thu, 08-Jul-2010 07:41:08 GMT; path=/; domain=.yahoo.com
    13. Set-Cookie: fpc=d=mE9W1x3vmwH5qQfykKO9i17Mi68As827SzEoRYZfeHl.7YKK16lgEMZn2AeBTCkH6rMVH2R7O3CIDu1vDGA9rMySC_7rhu_x1X_RXpUZsQFrhS_3gLr7VfouTwfBLAwisocjqWRT..pSmkbn_0UfXPfN1SxzCgHt9l5Sq8l4dyX7Cx21tKKImt_2DUe36ZK5cMj63JY-&v=2; expires=Sat, 07-Jul-2012 07:41:09 GMT; path=/; domain=www.yahoo.com
    14. Location: http://au.yahoo.com/?p=us
    15. Vary: Accept-Encoding
    16. Content-Type: text/html; charset=utf-8
    17. Age: 0
    18. Server: YTS/1.20.0
    19.  
    20. <!-- w27.fp.sp2.yahoo.com uncompressed/chunked Fri Jul 8 00:41:09 PDT 2011 -->
    21. Connection closed by foreign host.
    To copy to clipboard, switch view to plain text mode 
    This is a redirect, in my case to the Australian subsidiary. The only content in the response is the one line the OP is seeing.

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QNetworkAccessManager and http redirection

    Indeed. I forgot that QNetworkAccessManager sent the 'host' header.

Similar Threads

  1. qnetwork manager does not allow redirection?
    By JeanC in forum Qt Programming
    Replies: 1
    Last Post: 15th May 2011, 09:59
  2. HTTP Post from QNetworkAccessManager - no data sent
    By secureboot in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2011, 18:46
  3. QNetworkAccessManager Http Basic Authentication?
    By jloundy in forum Qt Programming
    Replies: 5
    Last Post: 29th December 2010, 00:19
  4. 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.