如何使用 QWebEngineUrlRequestInterceptor



我需要在Qt应用程序中拦截WebEngine qml组件中发出的请求。

我在QWebEngineUrlRequestInterceptor上找到了文档,它似乎在这里 http://doc.qt.io/qt-5/qwebengineprofile.html#details 和这里 http://doc.qt.io/qt-5/qwebengineurlrequestinterceptor.html#details 这样做。但是,我还没有找到有关如何在Qt/C++中执行此操作的任何代码示例。

任何帮助,不胜感激。

在这篇文章的帮助下设法将其拼凑在一起:https://forum.qt.io/topic/69135/how-to-send-network-requests-in-qwebengine/2

这是我连接QWebEngineUrlRequestInterceptor的解决方案:

.h 文件

class WebUrlRequestInterceptor : public QWebEngineUrlRequestInterceptor
{
    Q_OBJECT
public:
    WebUrlRequestInterceptor(QObject *p = Q_NULLPTR);
    void interceptRequest(QWebEngineUrlRequestInfo &info);
};

.cpp文件

WebUrlRequestInterceptor::WebUrlRequestInterceptor(QObject *p)
  :QWebEngineUrlRequestInterceptor(p)
{
}
void WebUrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) {
    QString rsrct = "";
    switch(info.resourceType()){
        case 0:rsrct="ResourceTypeMainFrame = 0, // top level page";break;
        case 1:rsrct="ResourceTypeSubFrame, // frame or iframe";break;
        case 2:rsrct="ResourceTypeStylesheet, // a CSS stylesheet";break;
        case 3:rsrct="ResourceTypeScript, // an external script";break;
        case 4:rsrct="ResourceTypeImage, // an image (jpg/gif/png/etc)";break;
        case 5:rsrct="ResourceTypeFontResource, // a font";break;
        case 6:rsrct="ResourceTypeSubResource, // an other subresource.";break;
        case 7:rsrct="ResourceTypeObject, // an object (or embed) tag for a plugin,";break;
        case 8:rsrct="ResourceTypeMedia, // a media resource.";break;
        case 9:rsrct="ResourceTypeWorker, // the main resource of a dedicated worker.";break;
        case 10:rsrct="ResourceTypeSharedWorker, // the main resource of a shared worker.";break;
        case 11:rsrct="ResourceTypePrefetch, // an explicitly requested prefetch";break;
        case 12:rsrct="ResourceTypeFavicon, // a favicon";break;
        case 13:rsrct="ResourceTypeXhr, // a XMLHttpRequest";break;
        case 14:rsrct="ResourceTypePing, // a ping request for <a ping>";break;
        case 15:rsrct="ResourceTypeServiceWorker, // the main resource of a service worker.";break;
        case 16:rsrct="ResourceTypeUnknown";break;
        default : rsrct="Unknown type";break;
    }
  qDebug()<<"t"<<Q_FUNC_INFO<<":ntt" << "WebUrlRequestInterceptor::interceptRequest    " <<info.requestMethod()
         <<"rn  "<<info.requestUrl()<<"   "<<rsrct      <<"rn";
}

主.cpp文件

WebUrlRequestInterceptor *wuri = new WebUrlRequestInterceptor();
QWebEngineProfile::defaultProfile()->setRequestInterceptor(wuri);

QWebEngineUrlRequestInterceptor 类为 URL 拦截提供了一个抽象的基类,我建议它来处理自定义协议,(mail://,example://....)

让我们通过子分支 QWebEngineUrlRequestInterceptor 来实现它:

您的头文件 exampleurlschemehandler.h 文件:

class ExampleUrlSchemeHandler : public QWebEngineUrlSchemeHandler
{
    Q_OBJECT
public:
    explicit ExampleUrlSchemeHandler(QObject *parent = 0);
public:
    void requestStarted(QWebEngineUrlRequestJob *request);
};

您的 cpp 文件:

ExampleUrlSchemeHandler::ExampleUrlSchemeHandler(QObject *parent) : QWebEngineUrlSchemeHandler(parent){}
void ExampleUrlSchemeHandler::requestStarted(QWebEngineUrlRequestJob *request){
    // Abort the request if you want to redirect it or something
    request->fail(QWebEngineUrlRequestJob::RequestAborted);
    // Get the URL
    const QUrl url = request->requestUrl();
    // Do amazing thing with your URL
    // .....
}

现在将其安装在默认的QWebEngineProfile中:

const QString EXAMPLE_SCHEMA_HANDLER = "example://" /* http://, https://, mail:// ....   */;
const QWebEngineUrlSchemeHandler* installed =  QWebEngineProfile::defaultProfile()->urlSchemeHandler(EXAMPLE_SCHEMA_HANDLER);
        if (!installed)
            profile()->installUrlSchemeHandler(EXAMPLE_SCHEMA_HANDLER, new WebAppUrlSchemeHandler(this));

处理HTTP/HTTPS请求的另一种方法是子类化QWebEnginePage,并重新实现acceptNavigationRequest():

bool WebAppPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool main) {
    // Example: We want to redirect all links clicked by user to native webbrowser
        if (type == QWebEnginePage::NavigationTypeLinkClicked)
                QDesktopServices::openUrl(url);
}

现在,在您的QWebEngineView中设置此页面。

相关内容

  • 没有找到相关文章