Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. Change Referrer in QT WebEngine View
Qt 6.11 is out! See what's new in the release blog

Change Referrer in QT WebEngine View

Scheduled Pinned Locked Moved Unsolved QtWebEngine
1 Posts 1 Posters 1.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • updatesvcU Offline
    updatesvcU Offline
    updatesvc
    wrote on last edited by updatesvc
    #1

    I am currently developing a web automation tool, I would like to know how to change the referrer.
    By referrer I mean the header variable stored by a browser when it visits a site .This variable usually contains the previous webpage link that lead to that site.

    So far I know with the QWebEngineUrlRequestInterceptor one can some how use it in your view but how can you change the referrer.Intuition says adding a HEADER somehow.

         QWebEngineUrlRequestInterceptor *interceptor;
        view->page()->profile()->setRequestInterceptor(interceptor);   
        view->page()->profile()->setHttpUserAgent("New User Agent");
    

    I have come accross this implemetation but i have no idea how to use it.

    #include "minibrowser.h"
    #include <QWebEngineProfile>
    #include <QWebEngineSettings>
    #include <QJsonObject>
    #include <QJsonDocument>
    #include <QWebEngineCookieStore>
    
    MiniBrowser::MiniBrowser(QObject *parent) : QWebEngineUrlRequestInterceptor(parent)
    {
        view = new QWebEngineView();
        connect(view, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
        connect(view->page()->profile()->cookieStore(), SIGNAL(cookieAdded(QNetworkCookie)), SLOT(cookieAdded(QNetworkCookie)));
        view->page()->profile()->setHttpCacheType(QWebEngineProfile::MemoryHttpCache);
        view->page()->profile()->setHttpUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36");
        view->page()->profile()->setRequestInterceptor(this);
        view->page()->settings()->setAttribute(QWebEngineSettings::AutoLoadImages, false);
        view->page()->settings()->setAttribute(QWebEngineSettings::AutoLoadIconsForPage, false);
        view->setUrl(QUrl("https://appleid.apple.com/account"));
        // view->show();
    }
    
    void MiniBrowser::interceptRequest(QWebEngineUrlRequestInfo &info)
    {
        QString file = info.requestUrl().fileName();
        if(file.endsWith(".woff") || file.endsWith(".ttf") || file.endsWith(".css") || file.endsWith("captcha")){
            info.block(true);
        }else{
            info.setHttpHeader("Accept-Language", "zh-CN,zh;q=0.8");
        }
    }
    
    void MiniBrowser::loadFinished(bool flag) {
        view->page()->toHtml([=](QString html){
            QJsonObject o;
    
            o.insert("html", QJsonValue(html));
            o.insert("cookies", cookies);
    
            QJsonDocument doc;
            doc.setObject(o);
            puts(doc.toJson(QJsonDocument::Compact).data());
            exit(0);
        });
    }
    
    void MiniBrowser::cookieAdded(const QNetworkCookie &cookie)
    {
        QJsonObject o;
        o.insert("name", QJsonValue(cookie.name().data()));
        o.insert("value", QJsonValue(cookie.value().data()));
        o.insert("domain", QJsonValue(cookie.domain()));
        o.insert("path", QJsonValue(cookie.path()));
        cookies.append(o);
    }
    

    project link for the above code can be found here

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved