QNetworkAccessManager问题

发布于 2024-11-26 23:58:10 字数 1465 浏览 1 评论 0原文

我正在尝试使用 QNetworkAccessManager 打开网页 - 对于某些页面它工作正常 - 而对于其他页面则不然。我尝试设置真正的浏览器用户代理,但它仍然不起作用,例如 http://www.erepublik.com 。代码如下:

QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
             this, SLOT(replyFinished(QNetworkReply*)));

QNetworkRequest *request = new QNetworkRequest(QUrl("http://www.erepublik.com"));

request->setRawHeader( "User-Agent", "Mozilla/5.0 (X11; U; Linux i686 (x86_64); "
                           "en-US; rv:1.9.0.1) Gecko/2008070206 Firefox/3.0.1" );
request->setRawHeader( "charset", "utf-8" );
request->setRawHeader( "Connection", "keep-alive" );

manager->get(*request);

...

void MainWindow::replyFinished(QNetworkReply *reply)
{
     QString data = reply->readAll();
     qDebug() << data;
}

数据如下:

<html><head><meta http-equiv="refresh" content="0;url=http://www.erepublik.com/en"/></head></html><html><head><meta http-equiv="refresh" content="0;url=http://www.erepublik.com/en"/></head></html>

现在,让我烦恼的是,这适用于像 http://www 这样的网站.hardwarebase.net(数据返回正常的 HTML 源),但它不适用于 eRepublik。

对于那些想知道我到底想做什么的人 - 我想从 eRepublik 主页获取国家/地区的人口数量。

有什么想法为什么会发生这种情况吗?提前致谢。

I'm trying to open a web page using QNetworkAccessManager - and for some pages it works fine - while for others it does not. I tried setting a real browser user-agent, however it still doesn't work for example, http://www.erepublik.com. Here's the code:

QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
             this, SLOT(replyFinished(QNetworkReply*)));

QNetworkRequest *request = new QNetworkRequest(QUrl("http://www.erepublik.com"));

request->setRawHeader( "User-Agent", "Mozilla/5.0 (X11; U; Linux i686 (x86_64); "
                           "en-US; rv:1.9.0.1) Gecko/2008070206 Firefox/3.0.1" );
request->setRawHeader( "charset", "utf-8" );
request->setRawHeader( "Connection", "keep-alive" );

manager->get(*request);

...

void MainWindow::replyFinished(QNetworkReply *reply)
{
     QString data = reply->readAll();
     qDebug() << data;
}

The data is the following:

<html><head><meta http-equiv="refresh" content="0;url=http://www.erepublik.com/en"/></head></html><html><head><meta http-equiv="refresh" content="0;url=http://www.erepublik.com/en"/></head></html>

Now, what's bugging me this works for a site like http://www.hardwarebase.net (data returns the normal HTML source), while it doesn't work for eRepublik.

For those who are curious to know what I exactly want to do - I want to get the population number of countries from the eRepublik front page.

Any ideas why is this happening? Thanks in advance.

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

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

发布评论

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

评论(2

沧桑㈠ 2024-12-03 23:58:10

看起来您正确获取了数据,只是该特定 URL 只是将您转发到另一个 URL。尝试使用 http://www.erepublik.com/en (使用 /en)。

It looks like you're getting the data correctly, it's just that that particular URL just forwards you to a different one. Try http://www.erepublik.com/en (with the /en) instead.

錯遇了你 2024-12-03 23:58:10

返回的 HTML 会将您重定向到 http://www.erepublik.com/en,因此您可以最好形成 QNetworkRequest 以直接访问该 URL。

The returned HTML is re-directing you to http://www.erepublik.com/en so you would be best off forming you QNetworkRequest to go straight to that URL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文