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
QtWS25 Last Chance

Change Referrer in QT WebEngine View

Scheduled Pinned Locked Moved Unsolved QtWebEngine
1 Posts 1 Posters 1.2k 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.
  • U Offline
    U Offline
    updatesvc
    wrote on 30 Jun 2017, 13:26 last edited by updatesvc 7 Jan 2017, 12:19
    #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

    1/1

    30 Jun 2017, 13:26

    • Login

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