Search code examples
c++qthttpqt4qnetworkaccessmanager

QNetworkAccessManager problem


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.


Solution

  • 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.