How to set user agent in Qt application

From Qt Wiki
Jump to navigation Jump to search

En Ar Bg De El Es Fa Fi Fr Hi Hu It Ja Kn Ko Ms Nl Pl Pt Ru Sq Th Tr Uk Zh

The HTTP User-Agent header identifies the client for the server in an HTTP communication session. Qt applications using HTTP either directly via QNetworkRequest APIs or indirectly via WebKit should define their own User-Agent that is used in the Qt Framework when doing the low-level communication with HTTP (S) servers.

If the application doesn't properly set the User-Agent header, the server has no means of identifying the application. This may also cause trouble for the application in mobile devices as operator gateways may not pass the applications network requests to the Internet.

Set your User-Agent header always to match your application name and version. Also, include in brackets the name of the device in which the application is running, for example "Nokia" when running in Nokia devices, to properly identify the application environment. Without this information your application network requests may not be passed through some cellular operator gateways. You may add additional token information when necessary.

As an example:

User-Agent: MyAppName/1.0 (Nokia)

When you are using Qt networking APIs to do networking configure the User-Agent in QNetworkRequest object:

QNetworkRequest request;
request.setUrl(QUrl("http://qt.nokia.com"));
request.setRawHeader("User-Agent", "MyAppName/1.0 (Nokia; Qt)");

The above configures the User-Agent header to be used in low-level network related classes for HTTP (S) networking operations. It doesn't impact to QWebKit. QWebKit has it's own default User-Agent which is explained in the QWebPage::userAgentForUrl method documentation. It looks something like this (running in Symbian device):

Mozilla/5.0 (Symbian; U; N8-00; fi-FI) AppleWebKit/534.3 (KHTML, like Gecko) Qt/4.7.4 Mobile Safari/534.3

For configuring fully custom User-Agent when using QtWebKit one has to subclass QWebPage. In the subclass implement QWebPage::userAgentForUrl where you define the wanted User-Agent string. Last you need to set the QWebPage subclass to the QWebView with setPage.

However, if application just needs to pass a specific product/version token pair as part of the default User-Agent header of QtWebKit then it can replace the default Qt/version part of the header with application specific one by simply setting an application and version strings to QApplication object as follows:

QApplication app(argc, argv);
app.setApplicationName(QString("MyOwnBrowserApp"));
app.setApplicationVersion(QString("1.0"));
//or use app.setApplicationVersion(QString("1.0 (Nokia; Qt)")) if you want to pass operator gateways in mobile

The above results in a User-Agent header in QtWebkit to be something like this:

Mozilla/5.0 (Symbian; U; N8-00; fi-FI) AppleWebKit/534.3 (KHTML, like Gecko) MyOwnBrowserApp/1.0 Mobile Safari/534.3

As of currently (Qt 4.7), this last mechanism is also the only easy way to configure User-Agent header when using QML WebView.