Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: How to get data from webkit requests

  1. #1
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to get data from webkit requests

    From the QNetworkReply docs:

    QNetworkReply is a sequential-access QIODevice, which means that once data is read from the object, it no longer kept by the device. It is therefore the application's responsibility to keep this data if it needs to.
    How can I save this data before it is read and lost?

    I tried catching the QNetworkAccessManager::finished() signal but usually (not always) the data was already lost.

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

    Default Re: How to get data from webkit requests

    What do you mean it was "already lost"? Did you read it in some other place?
    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.


  3. #3
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get data from webkit requests

    webkit would have read that data to help render the webpage

  4. #4
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get data from webkit requests

    Perhaps more explanation is needed. I am using QWebView to render webpages and have subclassed QNetworkAccessManager to try and save the data from certain requests, however the data is usually already read by the time of the QNetworkAccessManager::finished() signal.

    Would the solution be to override QNetworkAccessManager::createRequest() and create my own custom QNetworkReply objects that maintain the returned data?
    If so do you know of any examples?

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

    Default Re: How to get data from webkit requests

    If you even manage to intercept the data then WebKit won't be able to access it. Is that really what you want? Isn't it better to pick it up directly from WebKit's webpage object after it is downloaded and used?
    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.


  6. #6
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get data from webkit requests

    Quote Originally Posted by wysota View Post
    If you even manage to intercept the data then WebKit won't be able to access it. Is that really what you want?
    No. For example if I use peek() rather than read() then data is still there. Problem is peeking before webkit reads. Or overriding QNetworkReply so this is not a problem.


    Quote Originally Posted by wysota View Post
    Isn't it better to pick it up directly from WebKit's webpage object after it is downloaded and used?
    Unfortunately not possible in my use case because data is not retained.

  7. #7
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get data from webkit requests

    I struggled subclassing QNetworkReply - I don't sufficiently understand the internals.

    I am using the Python bindings so does that open up any additional possibilities?

    Or is there some way to just tell QIODevice to maintain the buffer?

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

    Default Re: How to get data from webkit requests

    Quote Originally Posted by rbp View Post
    Or is there some way to just tell QIODevice to maintain the buffer?
    The device is sequential. Once you read it, the data is gone.

    The only thing that I know will work which comes to my mind right now is to reimplement QNetworkAccessManager::createRequest() to return a custom QNetworkReply that will act as a proxy between a real network reply (which you have to create too) and the stuff that uses it (WebKit and your code). Then all calls to read() will be going through your network reply class so you can do anything you want with the data (i.e. copy it and provide elsewhere). I don't know if that's the best or the simplest approach but I know it will work (it might be quite a lot of work though).
    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.


  9. #9
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get data from webkit requests

    yeah I was trying to take that proxy approach, but feel I am working in the dark because am not familiar with QNetworkReply internals.

    I find that the html at the URL is downloaded correctly and the finished() signal is emitted, but the content of QWebView remains empty.
    How does QWebView get the content from QNetworkReply? Neither peek(), read(), readLine(), readData(), or readAll() is ever called in this class, but it will render properly when I use a standard QNetworkReply.

    Here is my code (adapted from this example):
    Qt Code:
    1. class NetworkReply(QNetworkReply):
    2. def __init__(self, reply):
    3. self.reply = reply
    4. QNetworkReply.__init__(self)
    5.  
    6. # handle these to forward
    7. reply.metaDataChanged.connect(self.applyMetaData)
    8. reply.readyRead.connect(self.readInternal)
    9. reply.error.connect(self.errorInternal)
    10. # forward signals
    11. reply.finished.connect(self.finished)
    12. reply.uploadProgress.connect(self.uploadProgress)
    13. reply.downloadProgress.connect(self.downloadProgress)
    14.  
    15. self.setOpenMode(QNetworkReply.ReadOnly)
    16. self.data = self.buffer = ''
    17.  
    18.  
    19. def operation(self):
    20. return self.reply.operation()
    21.  
    22.  
    23. def request(self):
    24. return self.reply.request()
    25.  
    26.  
    27. def url(self):
    28. return self.reply.url()
    29.  
    30.  
    31. def abort(self):
    32. self.reply.abort()
    33.  
    34. def close(self):
    35. self.reply.close()
    36.  
    37.  
    38. def isSequential(self):
    39. return self.reply.isSequential()
    40.  
    41.  
    42. def setReadBufferSize(self, size):
    43. QNetworkReply.setReadBufferSize(size)
    44. self.reply.setReadBufferSize(size)
    45.  
    46.  
    47. def applyMetaData(self):
    48. for header in self.reply.rawHeaderList():
    49. self.setRawHeader(header, self.reply.rawHeader(header))
    50.  
    51. self.setHeader(QNetworkRequest.ContentTypeHeader, self.reply.header(QNetworkRequest.ContentTypeHeader))
    52. self.setHeader(QNetworkRequest.ContentLengthHeader, self.reply.header(QNetworkRequest.ContentLengthHeader))
    53. self.setHeader(QNetworkRequest.LocationHeader, self.reply.header(QNetworkRequest.LocationHeader))
    54. self.setHeader(QNetworkRequest.LastModifiedHeader, self.reply.header(QNetworkRequest.LastModifiedHeader))
    55. self.setHeader(QNetworkRequest.SetCookieHeader, self.reply.header(QNetworkRequest.SetCookieHeader))
    56.  
    57. self.setAttribute(QNetworkRequest.HttpStatusCodeAttribute, self.reply.attribute(QNetworkRequest.HttpStatusCodeAttribute))
    58. self.setAttribute(QNetworkRequest.HttpReasonPhraseAttribute, self.reply.attribute(QNetworkRequest.HttpReasonPhraseAttribute))
    59. self.setAttribute(QNetworkRequest.RedirectionTargetAttribute, self.reply.attribute(QNetworkRequest.RedirectionTargetAttribute))
    60. self.setAttribute(QNetworkRequest.ConnectionEncryptedAttribute, self.reply.attribute(QNetworkRequest.ConnectionEncryptedAttribute))
    61. self.setAttribute(QNetworkRequest.CacheLoadControlAttribute, self.reply.attribute(QNetworkRequest.CacheLoadControlAttribute))
    62. self.setAttribute(QNetworkRequest.CacheSaveControlAttribute, self.reply.attribute(QNetworkRequest.CacheSaveControlAttribute))
    63. self.setAttribute(QNetworkRequest.SourceIsFromCacheAttribute, self.reply.attribute(QNetworkRequest.SourceIsFromCacheAttribute))
    64. # attribute does not exist
    65. #self.setAttribute(QNetworkRequest.DoNotBufferUploadDataAttribute, self.reply.attribute(QNetworkRequest.DoNotBufferUploadDataAttribute))
    66. self.metaDataChanged.emit()
    67.  
    68.  
    69. def errorInternal(self, e):
    70. self.error.emit(e)
    71. self.setError(e, str(e))
    72.  
    73.  
    74. def readInternal(self):
    75. # this is called
    76. s = self.reply.readAll()
    77. self.data += s
    78. self.buffer += s
    79. self.readyRead.emit()
    80.  
    81.  
    82. def bytesAvailable(self):
    83. return len(self.buffer) + self.reply.bytesAvailable()
    84.  
    85.  
    86. def readAll(self):
    87. # this is never called
    88. return self.data
    89.  
    90.  
    91. def readData(self, data, size):
    92. # this is never called
    93. size = min(size, len(self.buffer))
    94. data, self.buffer = self.buffer[:size], self.buffer[size:]
    95. return size
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get data from webkit requests

    figured it out - the docs are wrong. readData() only takes an int and returns a string:

    Qt Code:
    1. def readData(self, size):
    2. size = min(size, len(self.buffer))
    3. data, self.buffer = self.buffer[:size], self.buffer[size:]
    4. return str(data)
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: How to get data from webkit requests

    Quote Originally Posted by rbp View Post
    figured it out - the docs are wrong. readData() only takes an int and returns a string:
    Hmm? Where the docs are wrong?
    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.


  12. #12
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get data from webkit requests


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

    Default Re: How to get data from webkit requests

    Didn't you mix readData() with read()? The latter does what you say.
    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. #14
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get data from webkit requests

    Mix them how? My example wouldn't work until I defined readData() as above, which means that documentation is wrong.

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

    Default Re: How to get data from webkit requests

    No the docs are not wrong because Qt's QIODevice::readData() really takes two parameters. If something is wrong then it's the PyQt wrapping of that method.
    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.


  16. #16
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get data from webkit requests

    Yes you are right that Qt's readData() takes two parameters - I am not disputing that.
    However the Python bindings use a modified API to better suit the language, which is why I was able to guess the true syntax. The documentation is wrong.

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

    Default Re: How to get data from webkit requests

    In that case there should be no readData() method at all as it completely duplicates what read() does In C++ it makes sense to have two methods, in Python, it seems it doesn't.
    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.


  18. #18
    Join Date
    May 2009
    Posts
    133
    Thanks
    10
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get data from webkit requests

    Once I wanted to create a proxy for QNetworkReply too. That's when I started the thread Subclassing QNetworkReply.

    I'm curious; how did you find this example?
    Last edited by piotr.dobrogost; 15th December 2010 at 21:45.

  19. #19
    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    136
    Thanks
    9
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get data from webkit requests

    someone passed me that link on the Qt webkit mailing list.

  20. #20
    Join Date
    Apr 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get data from webkit requests

    Wow, this is *exactly* what I needed! I was in the same position, couldn't figure out how to correctly subclass QNetworkReply, but this did just the trick!

    Thanks a ton.

Similar Threads

  1. Interception of GET requests to the server
    By artkor in forum Newbie
    Replies: 4
    Last Post: 29th September 2010, 08:59
  2. Replies: 4
    Last Post: 23rd September 2010, 15:20
  3. Tracking multiple requests with QNetwork
    By rbp in forum Qt Programming
    Replies: 4
    Last Post: 1st June 2010, 08:04
  4. QTcpServer as a HTTPServer + POST Requests
    By NoRulez in forum Qt Programming
    Replies: 2
    Last Post: 27th May 2008, 17:49
  5. QHttp delaying requests
    By etru1927 in forum Qt Programming
    Replies: 8
    Last Post: 29th April 2008, 21:52

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
  •  
Qt is a trademark of The Qt Company.