Qwebengineview intercepting URL Request settings

Source: Internet
Author: User
Tags emit prefetch

Qwebengineview intercepting URL Request settings

The Qwebengineurlrequestinterceptor class provides an abstract base class for intercepting URL requests, which can be implemented by Qwebengineurlrequestinterceptor: The Interceptrequest interface intercepts and modifies the corresponding UIL request to achieve the desired effect, such as setting "Accept-language".

Class Twebengineurlrequestinterceptor:public qwebengineurlrequestinterceptor{    q_objectpublic:    Twebengineurlrequestinterceptor (Qobject *parent = q_nullptr);    void Interceptrequest (Qwebengineurlrequestinfo &info);};

Interception Interface Implementation

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://resource of shared worker Strinfo = "Resourcetypesharedworker";    Break        Case one://explicitly requested prefetch strinfo = "Resourcetypeprefetch";    Break        Case://favicon strinfo = "Resourcetypefavicon";    Break        Case://xml http request strinfo = "RESOURCETYPEXHR";    Break        Case://ping Request Strinfo = "resourcetypeping";    Break        Case://resource of service worker Strinfo = "Resourcetypeserviceworker";    Break        Case://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");}

Interface calls by setting the default Qwebengineprofile property

Twebengineurlrequestinterceptor *webinterceptor = new Twebengineurlrequestinterceptor (); Qwebengineprofile::d efaultprofile ()->setrequestinterceptor (webinterceptor);

  

Examples of your own actual code:

#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://resource of shared worker Strinfo = "Resourcetypesharedworker";    Break        Case one://explicitly requested prefetch strinfo = "Resourcetypeprefetch";    Break        Case://favicon strinfo = "Resourcetypefavicon";    Break        Case://xml http request strinfo = "RESOURCETYPEXHR";    Break        Case://ping Request Strinfo = "resourcetypeping";    Break        Case://resource of service worker Strinfo = "Resourcetypeserviceworker";    Break        Case://unknown Resource strinfo = "Resourcetypeunknown";    Break   Default:strinfo = "Unknown type";     Break }//qdebug () << "requestmethod===" << Info.requestmethod () << "\ n"//<< "requesturl===" &    lt;< 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 was true, so it would not be proceed. This function can is 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_objectpublic:    cmywebengineurlrequestinterceptor (QObject *parent = Q_ NULLPTR);    void Interceptrequest (Qwebengineurlrequestinfo &info);    BOOL Loaddownfileuiso ();}; #endif//Cmywebengineurlrequestinterceptor_h

  

    Cmywebengineurlrequestinterceptor *webinterceptor = new Cmywebengineurlrequestinterceptor ();    Qwebengineprofile::d efaultprofile ()->setrequestinterceptor (webinterceptor);

  

Qwebengineview intercepting URL Request settings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.