Webview user agent



  • Hello, I want to change the user agent of webview in qml, i found this link (https://bugreports.qt-project.org//browse/QTBUG-19259) where is showed how to do this step by step. I've created the files below,but i don't understand where i have to insert th user agent string.

    USERAGENTPROVIDER.cpp

    @#include "useragentprovider.h"

    UserAgentProvider::UserAgentProvider(QWidget *parent) :
    QWebPage(parent)
    {
    }

    QString UserAgentProvider::getUserAgent()
    {
    return userAgentForUrl(QUrl(""));
    }@

    CUSTOMNETWORKACCESSMANAGER.cpp

    @#include "customnetworkaccessmanager.h"
    #include "useragentprovider.h"
    #include <QNetworkReply>

    CustomNetworkAccessManager::CustomNetworkAccessManager(QString p_userAgent, QObject *parent) :
    QNetworkAccessManager(parent), __userAgent(p_userAgent)
    {
    }

    QNetworkReply *CustomNetworkAccessManager::createRequest( Operation op,
    const QNetworkRequest &req,
    QIODevice * outgoingData )
    {
    QNetworkRequest new_req(req);
    new_req.setRawHeader("User-Agent", __userAgent.toAscii());

    QNetworkReply *reply = QNetworkAccessManager::createRequest( op, new_req, outgoingData );
    return reply;
    

    }
    @

    networkaccessmanagerfactory.cpp

    @#include "networkaccessmanagerfactory.h"
    #include "customnetworkaccessmanager.h"

    NetworkAccessManagerFactory::NetworkAccessManagerFactory(QString p_userAgent) : QDeclarativeNetworkAccessManagerFactory(), __userAgent(p_userAgent)
    {
    }

    QNetworkAccessManager* NetworkAccessManagerFactory::create(QObject* parent)
    {
    CustomNetworkAccessManager* manager = new CustomNetworkAccessManager(__userAgent, parent);
    return manager;
    }@

    main.cpp
    @#include <QtGui/QApplication>
    #include "qmlapplicationviewer.h"
    #include "networkaccessmanagerfactory.h"
    #include <QtDeclarative>
    #include "useragentprovider.h"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    QmlApplicationViewer viewer;
    
    // Set custom NetworkAccessManagerFactory object in QDeclarative context
    UserAgentProvider p;
    QString userAgent = p.getUserAgent();
    
    NetworkAccessManagerFactory factory(userAgent);
    viewer.engine()->setNetworkAccessManagerFactory(&factory);
    
    viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
    viewer.setMainQmlFile(QLatin1String("qml/NetworkOverload/main.qml"&#41;&#41;;
    viewer.showExpanded(&#41;;
    
    return app.exec(&#41;;
    

    }@


  • Moderators


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.