QWebEngineUrlRequestInterceptor类提供了一个拦截URL请求抽象基类,可通过实现QWebEngineUrlRequestInterceptor::interceptRequest接口拦截并修改相应的UIL请求,以达到相应需求效果,比如设置“Accept-Language”。
class TWebEngineUrlRequestInterceptor : public QWebEngineUrlRequestInterceptor { Q_OBJECT public: TWebEngineUrlRequestInterceptor(QObject *parent = Q_NULLPTR); void interceptRequest(QWebEngineUrlRequestInfo &info); };
拦截接口实现
TWebEngineUrlRequestInterceptor::TWebEngineUrlRequestInterceptor(QObject *parent) : QWebEngineUrlRequestInterceptor(parent) { } void TWebEngineUrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) { QString strInfo = ""; switch (info.resourceType()) { case 0: //Top level page strInfo = "ResourceTypeMainFrame"; break; case 1: //Frame strInfo = "ResourceTypeSubFrame"; break; case 2: //CSS stylesheet strInfo = "ResourceTypeStylesheet"; break; case 3: //External script strInfo = "ResourceTypeScript"; break; case 4: //Image strInfo = "ResourceTypeImage"; break; case 5: //Font strInfo = "ResourceTypeFontResource"; break; case 6: //Sub-resource strInfo = "ResourceTypeSubResource"; break; case 7: //Plugin object strInfo = "ResourceTypeObject"; break; case 8: //Media resource strInfo = "ResourceTypeMedia"; break; case 9: //Resource of dedicated worker strInfo = "ResourceTypeWorker"; break; case 10: //Resource of shared worker strInfo = "ResourceTypeSharedWorker"; break; case 11: //Explicitly requested prefetch strInfo = "ResourceTypePrefetch"; break; case 12: //Favicon strInfo = "ResourceTypeFavicon"; break; case 13: //XML http request strInfo = "ResourceTypeXhr"; break; case 14: //Ping request strInfo = "ResourceTypePing"; break; case 15: //Resource of service worker strInfo = "ResourceTypeServiceWorker"; break; case 16: //Unknown resource strInfo = "ResourceTypeUnknown"; break; default: strInfo = "Unknown type"; break; } qDebug() << "RequestMethod===" << info.requestMethod() << "\n" << "RequestUrl===" << info.requestUrl() << "\n" << "RequestType===" << strInfo; //Set the value of the Accept-Language HTTP request-header. info.setHttpHeader("Accept-Language", "zh-CN"); }
接口调用,通过设置默认QWebEngineProfile属性
TWebEngineUrlRequestInterceptor *webInterceptor = new TWebEngineUrlRequestInterceptor(); QWebEngineProfile::defaultProfile()->setRequestInterceptor(webInterceptor);
自己的实际代码例子:
#include "cmywebengineurlrequestinterceptor.h" #include "QDebug" #include "../../vDownLoadUi/src/vIoDownfile_global.h" #include "../../common/common/common.h" #include "../../vIoMainUi/src/mainframe.h" extern MainFrame *g_pMainframe; Downfilewidget *g_DownFileDialog1 = NULL; extern Downfilewidget *g_DownFileDialog; CMyWebEngineUrlRequestInterceptor::CMyWebEngineUrlRequestInterceptor(QObject *parent) : QWebEngineUrlRequestInterceptor(parent) { } bool CMyWebEngineUrlRequestInterceptor::LoadDownfileUISO() { CreateObjectPtr CreateObjPtr = NULL; ReleaseObjectPtr ReleaseObjPtr = NULL; bool z_bRet = InitDllImport("vrvlib/libvDownLoadUi.so", CreateObjPtr, ReleaseObjPtr); if (!z_bRet || NULL == CreateObjPtr || NULL == ReleaseObjPtr) { QMessageBox msgBox; msgBox.setText("InitDllImport libvDownLoadUi.so failed."); msgBox.exec(); return false; } g_DownFileDialog1 = (Downfilewidget*)CreateObjPtr("DownLoadUi"); if (g_DownFileDialog1 == NULL) { QMessageBox msgBox; msgBox.setText("DownLoadUi new failed."); msgBox.exec(); return false; } return true; } bool g_test = false; void CMyWebEngineUrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) { // if (g_test == false) // { // g_test = true; // emit g_MainFrame->signal_ShowDownLoadUI("207830", 2); // //g_DownFileDialog->DownloadFile("207830", 2, 1, 1); // } QString strInfo = ""; switch (info.resourceType()) { case 0: //Top level page strInfo = "ResourceTypeMainFrame"; break; case 1: //Frame strInfo = "ResourceTypeSubFrame"; break; case 2: //CSS stylesheet strInfo = "ResourceTypeStylesheet"; break; case 3: //External script strInfo = "ResourceTypeScript"; break; case 4: //Image strInfo = "ResourceTypeImage"; break; case 5: //Font strInfo = "ResourceTypeFontResource"; break; case 6: //Sub-resource strInfo = "ResourceTypeSubResource"; break; case 7: //Plugin object strInfo = "ResourceTypeObject"; break; case 8: //Media resource strInfo = "ResourceTypeMedia"; break; case 9: //Resource of dedicated worker strInfo = "ResourceTypeWorker"; break; case 10: //Resource of shared worker strInfo = "ResourceTypeSharedWorker"; break; case 11: //Explicitly requested prefetch strInfo = "ResourceTypePrefetch"; break; case 12: //Favicon strInfo = "ResourceTypeFavicon"; break; case 13: //XML http request strInfo = "ResourceTypeXhr"; break; case 14: //Ping request strInfo = "ResourceTypePing"; break; case 15: //Resource of service worker strInfo = "ResourceTypeServiceWorker"; break; case 16: //Unknown resource strInfo = "ResourceTypeUnknown"; break; default: strInfo = "Unknown type"; break; } // qDebug()<< "RequestMethod===" << info.requestMethod() << "\n" // << "RequestUrl===" << info.requestUrl() << "\n" // << "RequestType===" << strInfo; QString z_TaskSerial = ""; int z_TaskType = 0; //?UserAccount=n2&TaskSerial=1234567&TaskType=1&DownAll=0 QUrl z_url(info.requestUrl()); QString z_strUrl = z_url.toString(); //QString z_strUrl = QString("downloadurl/downfile.html?UserAccount=n2&TaskSerial=1234567&TaskType=1&DownAll=0"); QString z_strDownloadurl = QString("downloadurl/downfile.html"); if(strstr(z_strUrl.toStdString().c_str(), z_strDownloadurl.toStdString().c_str())) { qDebug()<<"z_strUrl = "<<z_strUrl; QStringList z_StringList = z_strUrl.split(QRegExp("&")); QString z_Tmp = z_StringList.at(1); z_TaskSerial = z_Tmp.section(‘=‘, 1, 1); z_Tmp = z_StringList.at(2); z_TaskType = z_Tmp.section(‘=‘, 1, 1).toInt(); //LoadDownfileUISO(); if(g_DownFileDialog != NULL) { //g_DownFileDialog->DownloadFile(z_TaskSerial, z_TaskType, 1, 1); emit g_pMainframe->signal_ShowDownLoadUI(z_TaskSerial, z_TaskType); //g_DownFileDialog->DownloadFile("207830", 2, 1, 1); } } //Set the value of the Accept-Language HTTP request-header. // info.setHttpHeader("Accept-Language", "zh-CN"); // //Blocks this request if shouldBlock is true, so that it will not proceed. //This function can be used to prevent navigating away from a given domain, for example. // info.block(true); }
#ifndef CMYWEBENGINEURLREQUESTINTERCEPTOR_H #define CMYWEBENGINEURLREQUESTINTERCEPTOR_H #include <QWebEngineUrlRequestInterceptor> class CMyWebEngineUrlRequestInterceptor : public QWebEngineUrlRequestInterceptor { Q_OBJECT public: CMyWebEngineUrlRequestInterceptor(QObject *parent = Q_NULLPTR); void interceptRequest(QWebEngineUrlRequestInfo &info); bool LoadDownfileUISO(); }; #endif // CMYWEBENGINEURLREQUESTINTERCEPTOR_H
CMyWebEngineUrlRequestInterceptor *webInterceptor = new CMyWebEngineUrlRequestInterceptor(); QWebEngineProfile::defaultProfile()->setRequestInterceptor(webInterceptor);
时间: 2024-10-07 13:28:14