How to set user agent in Qt application: Difference between revisions
AutoSpider (talk | contribs) (Convert ExpressionEngine links) |
(Cleanup) |
||
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. | 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. | ||
Line 25: | 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 | 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.