How to send network requests in QWebEngine
-
Since QWebEngine does not interact with QNetworkAccessManager, is there any other way to send network requests and receive replies in QWebEngine?
For example, when WebKit was used, code looked like thisQNetworkAccessManager *p = getView()->page()->networkAccessManager();
-
http://doc.qt.io/qt-5/qtwebengine-overview.html
Qt WebEngine Core provides an API shared by Qt WebEngine and Qt WebEngine Widgets for handling URL requests issued for the networking stack of Chromium and for accessing its HTTP cookies.Implementing the QWebEngineUrlRequestInterceptor interface and installing the interceptor on a profile enables intercepting, blocking, and modifying URL requests (QWebEngineUrlRequestInfo) before they reach the networking stack of Chromium.
A QWebEngineUrlSchemeHandler can be registered for a profile to add support for custom URL schemes. Requests for the scheme are then issued to QWebEngineUrlSchemeHandler::requestStarted() as QWebEngineUrlRequestJob objects.
The QWebEngineCookieStore class provides functions for accessing HTTP cookies of Chromium. The functions can be used to synchronize cookies with QNetworkAccessManager, as well as to set, delete, and intercept cookies during navigation.
So,I Overwrite QWebEngineUrlRequestInfo:
#ifndef WEBURLREQUESTINTERCEPTOR_H #define WEBURLREQUESTINTERCEPTOR_H #include <QWebEngineUrlRequestInterceptor> class WebUrlRequestInterceptor : public QWebEngineUrlRequestInterceptor { Q_OBJECT public: WebUrlRequestInterceptor(QObject *p = Q_NULLPTR); void interceptRequest(QWebEngineUrlRequestInfo &info); }; #endif // WEBURLREQUESTINTERCEPTOR_H
#include "weburlrequestinterceptor.h" #include <QDebug> 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="未知类型";break; } qDebug()<<"\t"<<Q_FUNC_INFO<<":\n\t\t" << "WebUrlRequestInterceptor::interceptRequest " <<info.requestMethod() <<"\r\n "<<info.requestUrl()<<" "<<rsrct <<"\r\n"; }
overWrite QWebEnginePage:
WebPage::WebPage(QWebEngineProfile *profile, QObject *parent) : QWebEnginePage(profile, parent) { WebUrlRequestInterceptor *wuri = new WebUrlRequestInterceptor(); profile->setRequestInterceptor(wuri); }
And then,I can intercepting request.But I can't receive replies . Can you?
-
@mousemao I've made a feature request here https://bugreports.qt.io/browse/QTBUG-55074
BTW, 大哥,你好~~