Qt QWebView类自定义User-Agent

发布于 2024-09-27 03:18:20 字数 333 浏览 1 评论 0原文

有没有一种简单的方法来设置 QWebView 类正在使用的用户代理?

我发现搜索的唯一相关链接是这个

http://www.qtforum.org/article/27073/how-to-set-user-agent-in-qwebview.html

我现在正在学习 C++/Qt,但我不太明白什么是那个网站上有解释。也许有人知道一个简单的方法来做到这一点?或者可以帮助我理解该代码吗?

Is there an easy way to setup the User-Agent the QWebView class is using?

The only relevant link I found searching was this

http://www.qtforum.org/article/27073/how-to-set-user-agent-in-qwebview.html

I'm learning C++/Qt right now and I don't really understant what's explained on that website. Maybe someone knows an easy way to do it? Or can help me understand that code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵你的手,一向走下去 2024-10-04 03:18:20

Qt 允许您提供基于 URL 的用户代理,而不是无论 URL 是什么都提供单个用户代理。然后,想法是在创建新网页时随时返回用户代理:

class UserAgentWebPage : public QWebPage {
    QString userAgentForUrl(const QUrl &url ) const {
        return QString("My User Agent");
    }
};

为了使用该页面而不是创建的标准页面,您可以在发出请求之前在浏览器控制对象上设置该页面:

yourWebView->setPage(new UserAgentWebPage(parent));

我实际上会期望某个地方存在一个工厂,可以保证创建的网页始终属于某种类型,但我没有看到。

另一个选择应该是在 QNetworkRequest 中设置用户代理标头:

QNetworkRequest request = new QNetworkRequest();
request->setRawHeader(
    QString("User-Agent").toAscii(),
    QString("Your User Agent").toAscii()
    );
// ... set the URL, etc.
yourWebView->load(request);

您实际上想要检查是否它是 toAscii() 或 toUtf8() 或其他变体之一,因为我不确定 HTTP 标准到底允许什么。

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.

吃兔兔 2024-10-04 03:18:20

简单地,

class myWebPage : public QWebPage
{
    virtual QString userAgentForUrl(const QUrl& url) const {
        return "your user agent";
    }
};

//Attention here is new myWebPage() not new myWebPage(parent) 
UI->webView->setPage(new myWebPage());

simply,

class myWebPage : public QWebPage
{
    virtual QString userAgentForUrl(const QUrl& url) const {
        return "your user agent";
    }
};

//Attention here is new myWebPage() not new myWebPage(parent) 
UI->webView->setPage(new myWebPage());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文