QNetworkAccessManager问题
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您正确获取了数据,只是该特定 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.
返回的 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.