Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [Solved] How set user-agent in QWebView?

    Qt WebKit
    3
    3
    13555
    Loading More Posts
    • 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
      Pawelitel last edited by

      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 Reply Quote 1
      • G
        goetz last edited by

        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 Reply Quote 0
        • D
          dv879 last edited by

          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 Reply Quote 0
          • First post
            Last post