使用 QWebEngineView 捕获服务器响应 [英] Capture server response with QWebEngineView

查看:82
本文介绍了使用 QWebEngineView 捕获服务器响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Qt 中创建一个加载 URL 的对话框(我不想向最终用户公开,因此是一个对话框).一旦用户在页面上输入了他们的凭据,服务器就会返回一个我想要捕获的重定向 URL.我该怎么做?

I'm trying to create a Dialog in Qt which loads a URL (which I do not want to expose to the end-user, hence a Dialog). Once the user has entered their credentials on the page, the server returns a redirect URL which I want to capture. How can I do this?

QtWebkit 使这变得容易,因为 QWebView 有一个 QNetworkAccessManager 对象.但是对于 QtWebEngine,QWebEngineView 类没有这个能力.前者还允许通过使用 QNetworkRequest 类为任何请求设置 HTTP 标头,然后在 QWebView 中使用这些特定请求加载请求.如何使用 QWebEngineView 执行此操作?

QtWebkit made this easy to do as QWebView had a QNetworkAccessManager object. But with QtWebEngine, the QWebEngineView class does not have this capability. The former also allowed HTTP headers to be set for any requests by using the QNetworkRequest class and then load requests with these specific requests in QWebView. How do I do this with QWebEngineView?

推荐答案

自 Qt 5.6 起,针对您尝试使用 QWebEngineView 实现的目标,建议的解决方案是 QWebEngineUrlRequestInterceptor:

Since Qt 5.6 the proposed solution for what you are trying to achieve with QWebEngineView is QWebEngineUrlRequestInterceptor:

实现 QWebEngineUrlRequestInterceptor 接口并在配置文件上安装拦截器可以在 URL 请求到达 Chromium 的网络堆栈之前拦截、阻止和修改它们.

Implementing the QWebEngineUrlRequestInterceptor interface and installing the interceptor on the profile enables intercepting, blocking, and modifying URL requests before they reach the networking stack of Chromium.

它是一个抽象类,这意味着您需要对其进行子类化以获得您想要的:

It is an abstract class which means you need to subclass it to get what you want:

#include <QWebEngineUrlRequestInterceptor>
#include <QDebug>

class RequestInterceptor : public QWebEngineUrlRequestInterceptor
{
public:
    explicit RequestInterceptor(QObject * parent = Q_NULLPTR) : QWebEngineUrlRequestInterceptor(parent) {}
    virtual void interceptRequest(QWebEngineUrlRequestInfo & info) Q_DECL_OVERRIDE;
};

void RequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo & info)
{
    // Intercepting the requested URL
    QUrl url = info.requestUrl();
    qDebug() << "Request URL: " << url;

    // Optionally redirect the request URL but it only works for requests 
    // without payload data such as GET ones
    info.redirect(QUrl("https://www.google.com"));

    // Set HTTP header
    QByteArray httpHeaderName = "SomeHeaderName";
    QByteArray httpHeaderValue = "SomeHeaderValue";
    info.setHttpHeader(httpHeaderName, httpHeaderValue);
}

然后你需要在QWebEngineProfile中为特定的QWebEnginePage注册指向这个拦截器的指针,像这样:

Then you need to register the pointer to this interceptor in QWebEngineProfile for a particular QWebEnginePage, like this:

QWebEngineView * view = new QWebEngineView;
RequestInterceptor * interceptor = new RequestInterceptor(view);
QWebEngineProfile * profile = new QWebEngineProfile(view);
profile->setRequestInterceptor(interceptor);
QWebEnginePage * page = new QWebEnginePage(profile, view);
view->setPage(page);

这篇关于使用 QWebEngineView 捕获服务器响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆