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

  

时间: 2024-10-07 13:28:14

QWebEngineView拦截Url请求设置的相关文章

Gradle 多环境URL请求设置

在开发过程中,多环境配置是经常遇到的,比如在Android开发过程中,在不同环境上请求服务器的URL是不同的,使用Gradle进行管理,是非常方便的. 首先查看工程目录结构: 使用AndroidStudio开发的看到这个熟悉吧.main就是目前开发的环境.dev为测试环境.product,staging为其他环境,当然还可以有其他更多环境. 1.将请求的URL定义到Constant常量类中: public class Constant { public static final String U

Android Gradle 多环境URL请求设置

在开发过程中,多环境配置是经常遇到的,比如在Android开发过程中,在不同环境上请求服务器的URL是不同的,使用Gradle进行管理,是非常方便的. 首先查看工程目录结构: 使用AndroidStudio开发的看到这个熟悉吧.main就是目前开发的环境.dev为测试环境.product,staging为其他环境,当然还可以有其他更多环境. 1.将请求的URL定义到Constant常量类中: public class Constant { public static final String U

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

前言:今天在网上无意间看到cas单点登录排除请求的问题,发现很多人在讨论如何通过改写AuthenticationFilter类来实现忽略/排除请求URL的功能:突发奇想搜了一下,还真蛮多人都是这么干的,原谅我是个耿直的boy,当时我笑的饭都喷出来了,只需要一个配置的问题,被你们搞的这么麻烦:虽然很想回复他们"你们这帮人用别人的东西都不看源码的吗?",转念一想,这也要怪作者不给力,文档里压根没有提到这个配置,在这里用少量篇幅讲解如何配置排除不需要拦截的请求URL,后面用大量篇幅介绍我是如

iOS下JS与OC互相调用(一)--UIWebView 拦截URL

http://blog.csdn.net/u011619283/article/details/52135977 最近准备把之前用UIWebView实现的JS与原生相互调用功能,用WKWebView来替换.顺便搜索整理了一下JS 与OC 交互的方式,非常之多啊.目前我已知的JS 与 OC 交互的处理方式: * 1.在JS 中做一次URL跳转,然后在OC中拦截跳转.(这里分为UIWebView 和 WKWebView两种,去年因为还要兼容iOS 6,所以没办法只能采用UIWebView来做.) *

IOS拦截重定向请求(302)的几种方式

前言 在多数情况下,我们做的网络请求是返回200状态码的,但也有返回302的时候,比如使用基于Oauth2认证协议的API时,在认证阶段,需要提供一个回调地址,当用户授权后,服务器会返回一个302 Response,Response Header中会一个Location字段,包含了我们的回调地址,同时会有一个Code参数.我们在程序中该如何处理这个请求,并拿到这个Code参数呢.下面由我来为大家讲解下几种方式的做法,各取所需. 假设您知道并使用过Oauth2认证协议 (一)UIWebView控件

Spring Security 入门(1-9)Spring Security - 拦截 url

intercept-url配置 通过 pattern属性 指定拦截的 url 通过access属性指定url的访问权限 其中,access 的值是一个字符串,其可以直接是一个权限的定义,也可以是一个表达式. <security:http use-expressions="true">     <security:form-login />     <security:logout />     <security:intercept-url p

Postman 拦截浏览器请求

原文1:https://www.yuque.com/miyang.meyoung/other/mwikkg#603ee5e0 原文2:https://blog.csdn.net/qq598535550/article/details/82716564   扶墙建议查看该博客博主的Postman高级应用系列文章 1.app版不支持 https请求,可以使用chorme应用版,或者使用fiddle抓包,导出cURL script,再导入到postman也可以达到快速配置的目的 特别提醒:官网说了Po

根据URL请求 返回XML字符串

public static string GetHttpResponse(string url) { string content = ""; // Create a new HttpWebRequest object.Make sure that // a default proxy is set if you are behind a fure wall. //其中,HttpWebRequest实例不使用HttpWebRequest的构造函数来创建,二是使用WebRequest的C

【WebService】CXF拦截器的设置以及自定义CXF拦截器

WebService系列文章: [WebService]带你走进webservice的世界 [WebService]自定义WebService服务及其调用 [WebService]wsdl配置详解以及使用注解修改wsdl配置 [WebService]CXF处理javaBean等复合类型以及Map等复杂类型的数据 CXF的拦截器和以前学过的servlet的拦截器类似的,都是在开始或结束切入一段代码,执行一些逻辑之类的.我们可以在调用ws服务前设置拦截器,也可以在调用ws服务后设置拦截器,当然了,拦