如何在 QtWebKit 中获取响应

发布于 2024-08-24 08:45:51 字数 94 浏览 14 评论 0原文

我是 QtWebKit 的初学者,我构建了加载页面的简单 Web 框架(服务器端) 当我从这个页面提交数据时,我喜欢在 C++ 端捕获来自服务器的响应字符串 我该怎么做?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

善良天后 2024-08-31 08:45:51

我对 Qt(我是新手)进行了修改,找到了一种捕获 WebKit 下载的所有资源的方法。具体方法如下:

1) 创建您自己的 QNetworkAccessManager 子类

2) 在派生类中,重写虚函数 createRequest

3) 调用基类实现以获取响应对象。之后你可以看看
URL(或其他参数)并确定是否需要捕获该特定资源

4) 如果需要 - 将readyRead信号连接到某个将捕获数据的插槽

5) 在该插槽中调用peek函数来读取数据,以便WebKit也将获取数据

6) 创建 QWebPage 对象后,调用 setNetworkAccessManager 并传递步骤 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!

凌乱心跳 2024-08-31 08:45:51

您可以使用 QNetworkReply 类。 QWebPage 实例具有 networkAccessManager() 方法返回 QNetworkAccessManager 实例能够发送请求和接收响应。

您需要查找其 finished 信号。

void QNetworkAccessManager::finished (QNetworkReply * 回复)

每当
待处理的网络回复已完成。这
回复参数将包含一个指针
到刚刚完成的回复。

QNetworkReply 又是 QIODevice 因此您可以调用它的 readAll() 方法以接收响应数据。

您可能还会发现这个问题很有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文