Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

How to set user agent in Qt application: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
(Cleanup)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{LangSwitch}}
[[Category:HowTo]]
[[Category:HowTo]]
 
The [http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43 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.
= Set User-Agent Header in a Qt Application =
 
The "HTTP User-Agent header":http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43 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.
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.
Line 23: Line 21:
<code>Mozilla/5.0 (Symbian; U; N8-00; fi-FI) AppleWebKit/534.3 (KHTML, like Gecko) Qt/4.7.4 Mobile Safari/534.3</code>
<code>Mozilla/5.0 (Symbian; U; N8-00; fi-FI) AppleWebKit/534.3 (KHTML, like Gecko) Qt/4.7.4 Mobile Safari/534.3</code>


For configuring fully custom User-Agent when using "QtWebKit":http://doc.qt.nokia.com/4.7-snapshot/qtwebkit.html one has to subclass QWebPage. In the subclass implement "QWebPage::userAgentForUrl":http://doc.qt.nokia.com/4.7-snapshot/qwebpage.html#userAgentForUrl where you define the wanted User-Agent string. Last you need to set the QWebPage subclass to the "QWebView with setPage":http://doc.qt.nokia.com/4.7-snapshot/qwebview.html#setPage.
For configuring fully custom User-Agent when using {{DocLink|QtWebKit}} one has to subclass QWebPage. In the subclass implement [http://doc.qt.nokia.com/4.7-snapshot/qwebpage.html#userAgentForUrl QWebPage::userAgentForUrl] where you define the wanted User-Agent string. Last you need to set the QWebPage subclass to the [http://doc.qt.nokia.com/4.7-snapshot/qwebview.html#setPage 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:
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:

Latest revision as of 19:37, 28 June 2015

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.