How to set user agent in Qt application: Difference between revisions
No edit summary |
No edit summary |
||
Line 3: | Line 3: | ||
= Set User-Agent Header in a Qt Application = | = Set User-Agent Header in a Qt Application = | ||
The | 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. | ||
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 | 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:< | As an example: | ||
<code>User-Agent: MyAppName/1.0 (Nokia)</code> | |||
When you are using Qt networking APIs to do networking configure the User-Agent in QNetworkRequest object: | When you are using Qt networking APIs to do networking configure the User-Agent in QNetworkRequest object: | ||
<code> | |||
QNetworkRequest request; | |||
request.setUrl(QUrl("http://qt.nokia.com")); | |||
request.setRawHeader("User-Agent", "MyAppName/1.0 (Nokia; Qt)"); | |||
</code> | |||
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): | 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): | ||
<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 "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. | ||
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: | ||
<code> | |||
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 | |||
</code> | |||
The above results in a User-Agent header in QtWebkit to be something like this:< | The above results in a User-Agent header in QtWebkit to be something like this: | ||
<code>Mozilla/5.0 (Symbian; U; N8-00; fi-FI) AppleWebKit/534.3 (KHTML, like Gecko) MyOwnBrowserApp/1.0 Mobile Safari/534.3</code> | |||
As of currently (Qt 4.7), this last mechanism is also the only easy way to configure User-Agent header when using QML WebView. | As of currently (Qt 4.7), this last mechanism is also the only easy way to configure User-Agent header when using QML WebView. |
Revision as of 09:45, 25 February 2015
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.
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":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.
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.