Results 1 to 4 of 4

Thread: QT4 HTTP Example and Redirecting

  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QT4 HTTP Example and Redirecting

    Hi,
    I was trying the HTTP Example of qt4 documentation and noticed that it can't manage itself the status code 302, it can't perform a redirection.

    An example is this link:
    http://next.videogame.it/img/articol...e.cover.T5.jpg

    With the line below I get the new url:

    Qt Code:
    1. QString newLocation = responseHeader.value("Location");
    To copy to clipboard, switch view to plain text mode 

    But I don't know how to use it.

    Have anybody some tips for me?

    Thanks

    EDITED: HTTP Example
    Last edited by jiveaxe; 1st June 2008 at 17:03.
    Giuseppe CalÃ

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT4 HTTP Example and Redirecting

    When you receive the new location simply change the url of the request to the one you received in the location header and resubmit the request.

  3. #3
    Join Date
    Mar 2008
    Posts
    27
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT4 HTTP Example and Redirecting

    I had the same problem sometime ago.
    I solved it like this:

    Qt Code:
    1. self.connect(self.http, SIGNAL("responseHeaderReceived(const QHttpResponseHeader &)"), self.handleHeaderResponse)
    2.  
    3. [...]
    4.  
    5. def handleHeaderResponse(self, resp):
    6. code = resp.statusCode()
    7. if (code >= 300 and code < 400 and resp.hasKey("location")):
    8. location = QUrl(resp.value("location"))
    9. self.http.setHost(location.host())
    10. self.http.get(location.toString())
    To copy to clipboard, switch view to plain text mode 

    As you can see it's copied from my PyQt4 application... In C++ it should be something like this (not tested!)

    Qt Code:
    1. connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this, SLOT(handleHeaderResponse(const QHttpResponseHeader &)));
    2.  
    3. [...]
    4.  
    5. void MyClass::handleHeaderResponse(const QHttpResponseHeader &resp):
    6. int code = resp.statusCode();
    7. if (code >= 300 and code < 400 and resp.hasKey("location")) {
    8. QUrl location = QUrl(resp.value("location"));
    9. http.setHost(location.host());
    10. http.get(location.toString());
    11. }
    To copy to clipboard, switch view to plain text mode 

    Hope it helps you!

  4. #4
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QT4 HTTP Example and Redirecting

    I'm trying different ways but with no luck.

    First of all the code:
    Qt Code:
    1. class InternetJob: public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. InternetJob(const QString &u);
    7. QByteArray getData(){return data;}
    8. int getStatusCode(){return statusCode;}
    9. QString getLocation(){return location;}
    10. bool isRequestAborted(){return httpRequestAborted;}
    11. bool isRedirecting(){return needRedirect;}
    12. void download();
    13.  
    14. private slots:
    15. void httpRequestFinished(int requestId, bool error);
    16. void readResponseHeader(const QHttpResponseHeader &responseHeader);
    17.  
    18. signals:
    19. void requestFinished();
    20.  
    21. private:
    22. QHttp *http;
    23. QUrl url;
    24. bool httpRequestAborted;
    25. int httpGetId;
    26. QByteArray data;
    27. int statusCode;
    28. QString location;
    29. };
    30.  
    31. InternetJob::InternetJob(const QString &u)
    32. : url(u)
    33. {
    34. http = new QHttp;
    35. connect(http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), this, SLOT(readResponseHeader(const QHttpResponseHeader &)));
    36. connect(http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpRequestFinished(int, bool)));
    37.  
    38. download();
    39. }
    40.  
    41. void InternetJob::download()
    42. {
    43. QString query;
    44. for(int i = 0; i < url.queryItems().size(); ) {
    45. query += url.queryItems().at(i).first + "=" + url.queryItems().at(i).second;
    46. if(++i < url.queryItems().size())
    47. query += "&";
    48. }
    49.  
    50. if(url.queryItems().size() > 0)
    51. header.setRequest("GET", url.path() + "?" + QUrl::toPercentEncoding(query,"&="));
    52. else
    53. header.setRequest("GET", url.path());
    54. header.setValue("Host", url.host());
    55.  
    56. httpRequestAborted = false;
    57. http->setHost(url.host());
    58. httpGetId = http->request(header);
    59. qDebug() << "job id = " << httpGetId;
    60. }
    61.  
    62. void InternetJob::readResponseHeader(const QHttpResponseHeader &responseHeader)
    63. {
    64. statusCode = responseHeader.statusCode();
    65. QRegExp rx("\?$");
    66.  
    67. switch(statusCode) {
    68. case 200:
    69. case 301:
    70. case 303:
    71. case 307:
    72. break;
    73.  
    74. case 302:
    75. location = responseHeader.value("Location");
    76. // Remove trailing "?"
    77. location.remove(rx.indexIn(location), 1);
    78. httpRequestAborted = true;
    79. http->abort();
    80. break;
    81.  
    82. default:
    83. qDebug() << QString(trUtf8("Download failed: %1.").arg(responseHeader.reasonPhrase()));
    84. httpRequestAborted = true;
    85. http->abort();
    86. }
    87. }
    88.  
    89. void InternetJob::httpRequestFinished(int requestId, bool error)
    90. {
    91. if (requestId != httpGetId)
    92. return;
    93. qDebug() << "job finished = " << requestId;
    94. if (httpRequestAborted) {
    95. qDebug() << "Request Aborted. Status Code: " << statusCode;
    96. if(statusCode == 302) {
    97. url.setUrl(location);
    98. download();
    99. return;
    100. }
    101. else {
    102. emit requestFinished();
    103. return;
    104. }
    105. }
    106.  
    107. if(error)
    108. qDebug() << QString(trUtf8("Download failed: %1.").arg(http->errorString()));
    109. else
    110. data = http->readAll();
    111. emit requestFinished();
    112. }
    To copy to clipboard, switch view to plain text mode 

    The code above works well until the http status code is 200; if the status code is 302 it starts a new http->request(...) but after it neither responseHeaderReceived nor httpRequestFinished signals are emitted.

    Where is my mistake?

    Thanks
    Giuseppe CalÃ

Tags for this Thread

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.