QWebEngineView拦截Url请求设置

Posted www

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QWebEngineView拦截Url请求设置相关的知识,希望对你有一定的参考价值。

QWebEngineView拦截Url请求设置

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);

  

以上是关于QWebEngineView拦截Url请求设置的主要内容,如果未能解决你的问题,请参考以下文章

vue:axios拦截器

Spring拦截器拦截请求,获得加密的参数,怎样重新设置参数值?

axios设置请求拦截和响应拦截

如何制定filter不去拦截某类的url?

关于cas-client单点登录客户端拦截请求和忽略/排除不需要拦截的请求URL的问题(不需要修改任何代码,只需要一个配置)

关于cas-client单点登录客户端拦截请求和忽略/排除不需要拦截的请求URL的问题(不需要修改任何代码,只需要一个配置)