Qt allows you to provide a user agent based on the URL rather than a single user agent no matter what the URL is. The idea then is to return the user agent any time a new webpage is created:
class UserAgentWebPage : public QWebPage {
QString userAgentForUrl(const QUrl &url ) const {
return QString("My User Agent");
}
};
In order to use that page instead of the standard page that is created, you can set that page on the browser control object before making the request:
yourWebView->setPage(new UserAgentWebPage(parent));
I would actually expect a factory to be present somewhere that will guarantee that the webpage created is always of a certain type, but I didn't see one.
Yet another option should be to set the user agent header within the QNetworkRequest:
QNetworkRequest request = new QNetworkRequest();
request->setRawHeader(
QString("User-Agent").toAscii(),
QString("Your User Agent").toAscii()
);
// ... set the URL, etc.
yourWebView->load(request);
You would actually want to check whether it's toAscii()
or toUtf8()
or one of the other variants as I'm not sure exactly what the HTTP standard allows.