Page 2 of 2 FirstFirst 12
Results 21 to 34 of 34

Thread: QNetworkAccessManager and redirections

  1. #21
    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: QNetworkAccessManager and redirections

    Quote Originally Posted by alexandernst View Post
    That's because the app dies there.
    The content gets downloaded, it's even in the log you posted. Just check what's inside.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  2. The following user says thank you to wysota for this useful post:

    alexandernst (22nd September 2010)

  3. #22
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QNetworkAccessManager and redirections

    You're right. after the last "HEADER: xxxxxxxx", I get a file to the path that I specified in my code, but the content of the file is a HTML page. What does that mean?

  4. #23
    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: QNetworkAccessManager and redirections

    It means you should show us the contents of the page.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    alexandernst (22nd September 2010)

  6. #24
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QNetworkAccessManager and redirections


  7. #25
    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: QNetworkAccessManager and redirections

    What about the cookies? Did you return them with the request properly?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. The following user says thank you to wysota for this useful post:

    alexandernst (22nd September 2010)

  9. #26
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QNetworkAccessManager and redirections

    I had a look at wireshark but I can't see the second GET
    Can you see something wrong? http://pastebin.com/8VzT0JLc

  10. #27
    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: QNetworkAccessManager and redirections

    Please don't use pastebin. Attach files to your post instead.

    Please, do me a favour - dump headers of your second request to the console and show them to me.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    alexandernst (22nd September 2010)

  12. #28
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QNetworkAccessManager and redirections

    Please don't use pastebin. Attach files to your post instead.
    Ok, I'll do that from now on

    Please, do me a favour - dump headers of your second request to the console and show them to me.
    The log that I posted from my app in one of my posts is the entire output, and the entire output that QNetworkReply/QNAM can get me. Look at the part of my code where I output information.

    Qt Code:
    1. def downloadFinished(self, reply):
    2. redirect = reply.attribute(QNetworkRequest.RedirectionTargetAttribute).toUrl()
    3. print "ERROR CODE: " + str(reply.attribute(QNetworkRequest.HttpStatusCodeAttribute).toInt())
    4. for item in reply.rawHeaderList():
    5. print "HEADER: " + str(item) + " === " + reply.rawHeader(str(item))
    6. if not redirect.isEmpty():
    7. print "REDIRECT: " + str(redirect)
    8. self.reply = self.manager.get(QNetworkRequest(redirect))
    9. self.reply.downloadProgress.connect(self.updateDataReadProgress)
    10. else:
    11. self.updateFile = QFile(self.downloadPath)
    12. self.updateFile.open(QIODevice.WriteOnly)
    13. self.updateFile.write(self.reply.readAll())
    14. self.updateFile.close()
    15. self.reply.deleteLater()
    16. self.manager.deleteLater()
    17. reply.deleteLater()
    To copy to clipboard, switch view to plain text mode 

    As you can see, the second redirect's header list is printed item by item, and it gets only 1 item (the Content-Lenght one). Then evaluates the redirect.isEmpty(), and goes to the "else". And then saves the reply.readAll() to the file that I posted in #24.

  13. #29
    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: QNetworkAccessManager and redirections

    Quote Originally Posted by alexandernst View Post
    The log that I posted from my app in one of my posts is the entire output, and the entire output that QNetworkReply/QNAM can get me.
    I'm asking for the request, not for the reply.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. The following user says thank you to wysota for this useful post:

    alexandernst (22nd September 2010)

  15. #30
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QNetworkAccessManager and redirections

    I'm trying to get the request header but I'm not able to get it. I don't get anything with request.rawHeaderList().
    My actual code looks like this:

    Qt Code:
    1. def downloadFinished(self, reply):
    2. redirect = reply.attribute(QNetworkRequest.RedirectionTargetAttribute).toUrl()
    3. print "ERROR CODE: " + str(reply.attribute(QNetworkRequest.HttpStatusCodeAttribute).toInt())
    4. for item in reply.rawHeaderList():
    5. print "REPLY HEADER: " + str(item) + " === " + reply.rawHeader(str(item))
    6. request = QNetworkRequest(redirect)
    7. for item in request.rawHeaderList():
    8. print "REQUEST HEADER: " + str(item) + " === " + request.rawHeader(str(item))
    9. if not redirect.isEmpty():
    10. print "REDIRECT: " + str(redirect)
    11. self.reply = self.manager.get(request)
    12. self.reply.downloadProgress.connect(self.updateDataReadProgress)
    13. else:
    14. self.updateFile = QFile(self.downloadPath)
    15. self.updateFile.open(QIODevice.WriteOnly)
    16. self.updateFile.write(self.reply.readAll())
    17. self.updateFile.close()
    18. self.reply.deleteLater()
    19. self.manager.deleteLater()
    20. reply.deleteLater()
    To copy to clipboard, switch view to plain text mode 

    PS: Is it really that hard to make Qt act like urllib in python or wget/curl? Isn't really there something in Qt that will just work?

  16. #31
    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: QNetworkAccessManager and redirections

    Quote Originally Posted by alexandernst View Post
    PS: Is it really that hard to make Qt act like urllib in python or wget/curl?
    It is as hard as making wget/curl/urllib act like Qt.

    Isn't really there something in Qt that will just work?
    It does work.

    Edit: Try 'reply.request()' instead of 'request' (whatever that is).

    Don't blame Qt for your own mistakes.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  17. The following user says thank you to wysota for this useful post:

    alexandernst (22nd September 2010)

  18. #32
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QNetworkAccessManager and redirections

    Yep, that worked. Now I get the request header.

    Output:

    Qt Code:
    1. UPDATE: 163 of 163
    2. ERROR CODE: (302, True)
    3. REPLY HEADER: Server === nginx/0.7.63
    4. REPLY HEADER: Date === Wed, 22 Sep 2010 12:44:44 GMT
    5. REPLY HEADER: Content-Type === text/html; charset=UTF-8
    6. REPLY HEADER: Connection === close
    7. REPLY HEADER: Set-cookie === sf.consume=0fc84a4cf5c340bc4b568a7694abce97a8a762edgAJ9cQEoVQhfZXhwaXJlc3ECY2RhdGV0aW1lCmRhdGV0aW1lCnEDVQoH9gETAw4HAAAAhVJxBFUDX2lkcQVVIGMwNzM1ZWU5YzVlZjFlYjk4OTU5MWI0ZTg3YjZlZDRhcQZVDl9hY2Nlc3NlZF90aW1lcQdHQdMmfo8Q+CtVDl9jcmVhdGlvbl90aW1lcQhHQdMmfo8Q+Cd1Lg==; expires=Tue, 19-Jan-2038 03:14:07 GMT; Path=/
    8. REPLY HEADER: Location === http://sourceforge.net/projects/python-jake/files/betas/Jake-PyQT4-0.1.zip/download
    9. REPLY HEADER: Content-Length === 163
    10. REDIRECT: PyQt4.QtCore.QUrl(u'http://sourceforge.net/projects/python-jake/files/betas/Jake-PyQT4-0.1.zip/download')
    11. UPDATE: 697 of 17529
    12. UPDATE: 2145 of 17529
    13. UPDATE: 3593 of 17529
    14. UPDATE: 6489 of 17529
    15. UPDATE: 7937 of 17529
    16. UPDATE: 9385 of 17529
    17. UPDATE: 10833 of 17529
    18. UPDATE: 12281 of 17529
    19. UPDATE: 13729 of 17529
    20. UPDATE: 15177 of 17529
    21. UPDATE: 16625 of 17529
    22. UPDATE: 17529 of 17529
    23. ERROR CODE: (200, True)
    24. REPLY HEADER: Server === nginx/0.7.63
    25. REPLY HEADER: Date === Wed, 22 Sep 2010 12:44:47 GMT
    26. REPLY HEADER: Content-Type === text/html; charset=utf-8
    27. REPLY HEADER: Connection === close
    28. REPLY HEADER: Pragma === no-cache
    29. REPLY HEADER: Cache-Control === no-cache
    30. REPLY HEADER: Set-cookie === VISITOR=4c99fa3c86502b0357001464; expires="Sat, 19-Sep-2020 12:44:47 GMT"; httponly; Max-Age=315360000; Path=/
    31. sf.consume=be407794209dedb983a3e46c45e09112972ef415gAJ9cQEoVQhfZXhwaXJlc3ECY2RhdGV0aW1lCmRhdGV0aW1lCnEDVQoH9gETAw4HAAAAhVJxBFUFcHJlZnNxBX1xBlUOdXNlc19yZWxhdGlvbnNxB4lVB3ZlcnNpb25xCFUBMlUDa2V5cQlVGDRjOTlmYTNjODY1MDJiMDM1NzAwMTQ2NHEKVQNfaWRxC1UgZmRmOGIxMWEyZDQ2ZDkzMjljMTU1NjY5MTc0ZTJiMDdxDFUOX2FjY2Vzc2VkX3RpbWVxDUdB0yZ+j9wyalUOX2NyZWF0aW9uX3RpbWVxDkdB0yZ+j9wxanUu; expires=Tue, 19-Jan-2038 03:14:07 GMT; Path=/
    32. REPLY HEADER: Content-Length === 17529
    33. REQUEST HEADER: Cookie === sf.consume=0fc84a4cf5c340bc4b568a7694abce97a8a762edgAJ9cQEoVQhfZXhwaXJlc3ECY2RhdGV0aW1lCmRhdGV0aW1lCnEDVQoH9gETAw4HAAAAhVJxBFUDX2lkcQVVIGMwNzM1ZWU5YzVlZjFlYjk4OTU5MWI0ZTg3YjZlZDRhcQZVDl9hY2Nlc3NlZF90aW1lcQdHQdMmfo8Q+CtVDl9jcmVhdGlvbl90aW1lcQhHQdMmfo8Q+Cd1Lg==
    34. ERROR CODE: (200, True)
    35. REPLY HEADER: Server === nginx/0.7.63
    36. REPLY HEADER: Date === Wed, 22 Sep 2010 12:44:47 GMT
    37. REPLY HEADER: Content-Type === text/html; charset=utf-8
    38. REPLY HEADER: Connection === close
    39. REPLY HEADER: Pragma === no-cache
    40. REPLY HEADER: Cache-Control === no-cache
    41. REPLY HEADER: Set-cookie === VISITOR=4c99fa3c86502b0357001464; expires="Sat, 19-Sep-2020 12:44:47 GMT"; httponly; Max-Age=315360000; Path=/
    42. sf.consume=be407794209dedb983a3e46c45e09112972ef415gAJ9cQEoVQhfZXhwaXJlc3ECY2RhdGV0aW1lCmRhdGV0aW1lCnEDVQoH9gETAw4HAAAAhVJxBFUFcHJlZnNxBX1xBlUOdXNlc19yZWxhdGlvbnNxB4lVB3ZlcnNpb25xCFUBMlUDa2V5cQlVGDRjOTlmYTNjODY1MDJiMDM1NzAwMTQ2NHEKVQNfaWRxC1UgZmRmOGIxMWEyZDQ2ZDkzMjljMTU1NjY5MTc0ZTJiMDdxDFUOX2FjY2Vzc2VkX3RpbWVxDUdB0yZ+j9wyalUOX2NyZWF0aW9uX3RpbWVxDkdB0yZ+j9wxanUu; expires=Tue, 19-Jan-2038 03:14:07 GMT; Path=/
    43. REPLY HEADER: Content-Length === 17529
    44. REQUEST HEADER: Cookie === sf.consume=0fc84a4cf5c340bc4b568a7694abce97a8a762edgAJ9cQEoVQhfZXhwaXJlc3ECY2RhdGV0aW1lCmRhdGV0aW1lCnEDVQoH9gETAw4HAAAAhVJxBFUDX2lkcQVVIGMwNzM1ZWU5YzVlZjFlYjk4OTU5MWI0ZTg3YjZlZDRhcQZVDl9hY2Nlc3NlZF90aW1lcQdHQdMmfo8Q+CtVDl9jcmVhdGlvbl90aW1lcQhHQdMmfo8Q+Cd1Lg==
    To copy to clipboard, switch view to plain text mode 

    Code:

    Qt Code:
    1. def downloadFinished(self, reply):
    2. redirect = reply.attribute(QNetworkRequest.RedirectionTargetAttribute).toUrl()
    3. print "ERROR CODE: " + str(reply.attribute(QNetworkRequest.HttpStatusCodeAttribute).toInt())
    4. for item in reply.rawHeaderList():
    5. print "REPLY HEADER: " + str(item) + " === " + reply.rawHeader(str(item))
    6. for item in reply.request().rawHeaderList():
    7. print "REQUEST HEADER: " + str(item) + " === " + reply.request().rawHeader(str(item))
    8. if not redirect.isEmpty():
    9. print "REDIRECT: " + str(redirect)
    10. request = QNetworkRequest(redirect)
    11. self.reply = self.manager.get(request)
    12. self.reply.downloadProgress.connect(self.updateDataReadProgress)
    13. else:
    14. self.updateFile = QFile(self.downloadPath)
    15. self.updateFile.open(QIODevice.WriteOnly)
    16. self.updateFile.write(self.reply.readAll())
    17. self.updateFile.close()
    18. self.reply.deleteLater()
    19. self.manager.deleteLater()
    20. reply.deleteLater()
    To copy to clipboard, switch view to plain text mode 

    So, I do send the cookie, but the app just stays there. No progress, no further replies, no nothing.

  19. #33
    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: QNetworkAccessManager and redirections

    Ok but what further reply would you expect? The webpage you are getting is setup in a way that after a couple of seconds a (probably javascript based) event kicks in and redirects you elsewhere.

    You are probably not sending some header sf.net requires to redirect you automatically without the waiting period. It could be a user-agent, for instance. Compare the traffic between wget and sf.net with that between your application and sf.net. Try sending the same headers and see what reply you get.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  20. The following user says thank you to wysota for this useful post:

    alexandernst (22nd September 2010)

  21. #34
    Join Date
    Jul 2009
    Location
    Valladolid, Spain
    Posts
    125
    Thanks
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QNetworkAccessManager and redirections

    Finally!!!
    It's working!!!!!!
    I set (as you suggested) the user-agent to wget's user-agent and now it's working just fine

    Here is the complete code:

    Qt Code:
    1. # -*- coding: utf-8 -*-
    2. import sys, os
    3. from PyQt4 import QtCore, QtGui
    4. from PyQt4.QtCore import *
    5. from PyQt4.QtGui import *
    6. from PyQt4.QtNetwork import *
    7. class QDownloader(QProgressDialog):
    8. def __init__(self, parent=None):
    9. QProgressDialog.__init__(self, parent)
    10. self.parent = parent
    11. self.url = "https://sourceforge.net/projects/python-jake/files/betas/Jake-PyQT4-0.1.zip/download"
    12. self.downloadPath = "/tmp/tmpreadfile"
    13. self.manager = QNetworkAccessManager()
    14. self.jar = QNetworkCookieJar()
    15. self.manager.setCookieJar(self.jar)
    16. self.resize(self.size().width()*2, self.size().height())
    17. self.manager.finished.connect(self.downloadFinished)
    18. self.canceled.connect(self.cancelDownload)
    19. self.show()
    20. self.download()
    21.  
    22. def download(self):
    23. self.url = QUrl(self.url)
    24. self.request = QNetworkRequest(self.url)
    25. self.request.setRawHeader("User-Agent", "Wget/1.12 (linux-gnu)")
    26. self.reply = self.manager.get(self.request)
    27. self.reply.downloadProgress.connect(self.updateDataReadProgress)
    28.  
    29. def updateDataReadProgress(self, done, total):
    30. self.setMaximum(total)
    31. self.setValue(done)
    32.  
    33. def downloadFinished(self, reply):
    34. self.redirect = reply.attribute(QNetworkRequest.RedirectionTargetAttribute).toUrl()
    35. if not self.redirect.isEmpty():
    36. self.request = QNetworkRequest(self.redirect)
    37. self.request.setRawHeader("User-Agent", "Wget/1.12 (linux-gnu)")
    38. self.reply = self.manager.get(self.request)
    39. self.reply.downloadProgress.connect(self.updateDataReadProgress)
    40. else:
    41. self.updateFile = QFile(self.downloadPath)
    42. self.updateFile.open(QIODevice.WriteOnly)
    43. self.updateFile.write(self.reply.readAll())
    44. self.updateFile.close()
    45. self.reply.deleteLater()
    46. self.manager.deleteLater()
    47. self.close()
    48. reply.deleteLater()
    49.  
    50. def cancelDownload(self):
    51. self.reply.abort()
    52.  
    53. app = QtGui.QApplication(sys.argv)
    54. window = QDownloader()
    55. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QNetworkAccessManager within QThread
    By Tomdarkness in forum Newbie
    Replies: 17
    Last Post: 26th August 2014, 15:12
  2. qnetworkaccessmanager problem!
    By novamaster in forum Qt Programming
    Replies: 6
    Last Post: 7th August 2010, 11:46
  3. QNetworkAccessManager question
    By _Stefan in forum Qt Programming
    Replies: 1
    Last Post: 5th March 2010, 12:21
  4. QNetworkAccessManager or QHttp
    By mind_freak in forum Qt Programming
    Replies: 3
    Last Post: 29th September 2009, 20:24
  5. How to Login using QNetworkAccessManager?
    By cydside in forum Newbie
    Replies: 1
    Last Post: 31st August 2009, 21:41

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.