我是 QtWebKit 的初学者,我构建了加载页面(服务器端)的简单 web 框架,当我从这个页面提交数据时,我想从 c++ 端的服务器捕获响应字符串,我该怎么做?
2 回答
我对 Qt(我是新手)进行了修改,并找到了一种方法来捕获 WebKit 下载的所有资源。就是这样:
1) 创建自己的 QNetworkAccessManager 子类
2)在您的派生类中,覆盖虚函数 createRequest
3) 调用基类实现获取响应对象。之后,您可以查看 URL(或其他参数)并确定是否需要捕获该特定资源
4)如果你这样做 - 将 readyRead 信号连接到将捕获数据的某个插槽
5)在那个槽调用 peek 函数来读取数据,这样 WebKit 也将获取数据
6) 创建 QWebPage 对象后,调用 setNetworkAccessManager 并传递步骤 1 中新创建的子类实例)
就是这样——享受吧!
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.