PDA

View Full Version : How to cache all network traffic including POST?



rbp
7th June 2016, 17:58
Hello,

I have a QWebView and want to cache all network traffic so that can interact with the exact same webpage later when offline.

I first looked at QNetworkDiskCache and I understand would need to set PreferCache and then override the Expires header to avoid the content expiring. However the cache interface just takes a QUrl, so would only be for GET requests and I also need to cache POST requests.

If I override QNetworkAccessManager::createRequest is there a way to create a QNetworkReply instance that already has the content instead of downloading? If so then the network traffic could be manually cached and loaded here.

thanks,
Richard

anda_skoa
8th June 2016, 20:24
Well, QNetworkReply is a QIODevice, so it should be possible to override the readData/writeData methods.
It mostly depends if you can intercept/manipulate its state handling as well.

Cheers,
_

rbp
9th June 2016, 04:26
That is a good idea, however wouldn't Qt still be downloading the content and emitting errors when offline?

anda_skoa
9th June 2016, 08:03
Well, that would depend on what your QNetworkReply implementation would be doing.

Cheers,
_

rbp
9th June 2016, 17:06
So is it possible to prevent the QNetworkReply downloading?

anda_skoa
9th June 2016, 17:48
So is it possible to prevent the QNetworkReply downloading?

Well, you are the one implementing a custom QNetworkReply subclass, you tell us.

Cheers,
_

rbp
9th June 2016, 19:38
not sure if you meant to be snarky, but I didn't see an example for this case or method in the documentation that would allow controlling network activity.
Does anyone else know? Or is there a proper way to approach caching rather than hacking QIDevice?

anda_skoa
9th June 2016, 21:00
I had assumed that you had tried or at least had a look at the implementation in QNetworkAccessManager, but you are of course welcome to wait.

Cheers,
_

rbp
11th June 2016, 11:54
yes you assume right, which should be obvious from initial post...

I found some similar work, so hopefully can build off it:
https://blogs.kde.org/2010/08/28/implementing-reusable-custom-qnetworkreply