how to get response in QtWebKit

耗尽温柔 提交于 2019-12-18 04:17:10

问题


im beginner with QtWebKit i build simple web frame that loaded page ( server side ) and when from this page i submit data i like to catch the response string from the server in the c++ side how can i do that ?


回答1:


I tinkered around with Qt (which I'm new to) and found a way to catch all resources downloaded by WebKit. Here's how:

1) Create your own subclass of QNetworkAccessManager

2) In your derived class, override virtual function createRequest

3) Call base class implementation to get the response object. After that you can look at the URL (or other parameters) and determine whether you need to capture that particular resource or not

4) if you do - connect readyRead signal to some slot that will capture the data

5) in that slot call peek function to read data so that WebKit will get the data also

6) After creating QWebPage object, call setNetworkAccessManager and pass a newly created instance of your subclass from step 1)

That's it - enjoy!




回答2:


You can use QNetworkReply class for it. QWebPage instances have networkAccessManager() method that returns a QNetworkAccessManager instance capable of sending requests and receiving responses.

You need to look for its finished signal.

void QNetworkAccessManager::finished ( QNetworkReply * reply )

This signal is emitted whenever a pending network reply is finished. The reply parameter will contain a pointer to the reply that has just finished.

QNetworkReply in its turn is an inheritor of QIODevice therefore you are able to call its readAll() method in order to receive the response data.

You may also find this question useful.



来源:https://stackoverflow.com/questions/2426053/how-to-get-response-in-qtwebkit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!