Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt WebKit
  4. [Solved] How set user-agent in QWebView?
QtWS25 Last Chance

[Solved] How set user-agent in QWebView?

Scheduled Pinned Locked Moved Qt WebKit
3 Posts 3 Posters 13.9k 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.
  • P Offline
    P Offline
    Pawelitel
    wrote on last edited by
    #1

    How can I change HTTP_USER_AGENT used by QWebView?
    In half-measure I can change name and version of application, such as
    @ app.setApplicationName(QString("Chrome"));
    app.setApplicationVersion(QString("15.0.874.121"));@
    but I can not change version of WebKit and OS. Ideally I would like to completely change HTTP_USER_AGENT and HTTP_ACCEPT_LANGUAGE to arbitrary values ​​for QWebView.

    I can download individual pages by using the
    @void load ( const QNetworkRequest & request, QNetworkAccessManager::Operation operation = QNetworkAccessManager::GetOperation, const QByteArray & body = QByteArray() )@
    and in QNetworkRequest change setRawHeader
    but how do I replace HTTP_USER_AGENT for any user action?
    As alternative i can make proxy, which changes HTTP_USER_AGENT and HTTP_ACCEPT_LANGUAGE

    UPDATE
    Volker, thanks, it work
    My implementation:
    @class QWebPageChrome : public QWebPage
    {
    Q_OBJECT
    public:
    QString userAgentForUrl ( const QUrl & url ) const;
    };

    class QWebPageFirefox : public QWebPage
    {
    Q_OBJECT
    public:
    QString userAgentForUrl ( const QUrl & url ) const;
    };@

    @QString QWebPageChrome::userAgentForUrl ( const QUrl & url ) const
    {
    return "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
    }
    QString QWebPageFirefox::userAgentForUrl ( const QUrl & url ) const
    {
    return "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:8.0) Gecko/20100101 Firefox/8.0";
    }@

    @ QWebView *chromeView = new QWebView(this);
    QWebView *firefoxView = new QWebView(this);
    QWebPageChrome *pageChrome= new QWebPageChrome();
    QWebPageFirefox *pageFirefox= new QWebPageFirefox();
    chromeView->setPage(pageChrome);
    firefoxView->setPage(pageFirefox);@

    live long and prosper

    1 Reply Last reply
    1
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #2

      Have a look at "QWebPage::userAgentForUrl() ":http://doc.qt.nokia.com/4.7/qwebpage.html#userAgentForUrl

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dv879
        wrote on last edited by
        #3

        I just wanted to thank you guys! Volker, for sharing your knowledge and Pawelitel for sharing his implementation. Works like a charm! :)

        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