如何使用 QWebEngineUrlRequestInterceptor
我需要在 Qt 应用程序的 WebEngine qml 组件中拦截请求.
I need to intercept requests made in our WebEngine qml component in our Qt application.
我找到了关于 QWebEngineUrlRequestInterceptor 的文档,这似乎就是在这里 http:///doc.qt.io/qt-5/qwebengineprofile.html#details 和这里 http://doc.qt.io/qt-5/qwebengineurlrequestinterceptor.html#details.但是我还没有找到任何关于如何在 Qt/C++ 中执行此操作的代码示例.
I have found documentation on QWebEngineUrlRequestInterceptor which seems to do just this here http://doc.qt.io/qt-5/qwebengineprofile.html#details and here http://doc.qt.io/qt-5/qwebengineurlrequestinterceptor.html#details. However I haven't found any code examples on how to do this in Qt/C++.
感谢任何帮助.
推荐答案
在这篇文章的帮助下设法解决了这个问题:https://forum.qt.io/topic/69135/how-to-send-network-requests-in-qwebengine/2
Managed to puzzle it together with the help of this post: https://forum.qt.io/topic/69135/how-to-send-network-requests-in-qwebengine/2
这是我连接 QWebEngineUrlRequestInterceptor 的解决方案:
This is my solution to hook up a 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()<<" "<<Q_FUNC_INFO<<":
" << "WebUrlRequestInterceptor::interceptRequest " <<info.requestMethod()
<<"
"<<info.requestUrl()<<" "<<rsrct <<"
";
}
main.cpp 文件
main.cpp file
WebUrlRequestInterceptor *wuri = new WebUrlRequestInterceptor();
QWebEngineProfile::defaultProfile()->setRequestInterceptor(wuri);
相关文章