I was searching for a method to access & tamper the raw packet data sent and received by a Qt application but could not find anything. Is there any method? Or if not the application at least a method to access the packet data from a QWebView.
Is there any method to achieve any of the above two?
I'm sorry, Sam, I cannot make it work. I've been trying everything I can imagine, but nothing goes as it would have been desired.
Let's make a short list of options and what can and cannot be done:
You can access the retrieved web page by means of obtaining the QWebFrame
with webView->page()->currentFrame()
and then accessing to methods like setContents()
, setHtml()
, toHtml()
or toPlainText()
. You can, by those functions, change dynamically the web page shown in the widget.
You can access the raw response (as an array of bytes) received for each request, by means of the method reply->readAll()
in the replyFinished()
slot.
It's possible to intercept the requests, by overriding the QNetworkAccessManager::createRequest()
method in a network access manager of your own. There you can access the URL and the headers sent in the request and change them or cancel the request.
But... you want to access to the request and replied data for each request.
Access to the request is easy, it's already been explained by the override to the createRequest()
method in a custom QNetworkAccessManager
. But let's talk about the other part:
For what I've seen, the reply obtained from the request is read-only and IT CAN'T BE CHANGED, as it's hard-coded in the source code for QNetworkReply
.
You cannot use the writeData()
function of QNetworkReply
, as it is hardcoded to simply do return -1
!!!
You can try to subclass a QNetworkReply
of your own and return it in the createRequest()
method of a custom QNetworkAccessManager
. You must override the functions readAll()
, bytesAvailable()
, and abort()
. Curiously, this method works only if the request is made to a non-HTTP destination. I think the internals of QNetworkAccessManager
invokes different implementations of the QNetworkReply
depending of the protocol used (HTTP, FTP, ...). So this worked in a sense, but not in the way we want. This method can be used to return a predefined web page under certain conditions, for example.
Considering the fact that QNetworkAccessManager::createRequest()
must return a QNetworkReply
object, I cannot see any combination of subclassing that permits tampering in the replied byte array. It's really well protected and you must reimplement everything on those classes to be able to achieve your goal. Almost copy-paste the source classes of all the QtNetwork
part and make your own implementation.
So I think that the answer would be: "No, it seems that it cannot be done exactly as you ask; only partially".
Sorry.