views:

295

answers:

1

I'm trying to use Qt to download the html code from the following url:

http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=nucleotide&cmd=search&term=AB100362

this url will re-direct to

www.ncbi.nlm.nih.gov/nuccore/27884304

I try to do it by following way, but I cannot get anything. it works for some webpage such as www.google.com, but not for this NCBI page. is there any way to get this page??

QNetworkReply::NetworkError downloadURL(const QUrl &url, QByteArray &data)
{
    QNetworkAccessManager manager;
    QNetworkRequest request(url);
    QNetworkReply *reply = manager.get(request);

    QEventLoop loop;
    QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
    loop.exec();

    if (reply->error() != QNetworkReply::NoError)
    {
        return reply->error();
    }
    data = reply->readAll();
    delete reply;
    return QNetworkReply::NoError;
}

void GetGi()
{
        int pos;

        QString sGetFromURL = "http://www.ncbi.nlm.nih.gov/entrez/query.fcgi";
        QUrl url(sGetFromURL);
        url.addQueryItem("db", "nucleotide");
        url.addQueryItem("cmd", "search");
        url.addQueryItem("term", "AB100362");

        QByteArray InfoNCBI;
        int errorCode = downloadURL(url, InfoNCBI);
        if (errorCode != 0 )
        {
            QMessageBox::about(0,tr("Internet Error "), tr("Internet Error %1: Failed to connect to NCBI.\t\nPlease check your internect connection.").arg(errorCode));
            return "ERROR";
        }

}
+3  A: 

That page appears to have a redirect.

From the Qt docs for 4.6:

Note: When the HTTP protocol returns a redirect no error will be reported. You can check if there is a redirect with the QNetworkRequest::RedirectionTargetAttribute attribute.

Evan Teran
Thanks :)~ It's really helpful.However, I've try to use the redirection, it works- I can get some html code which belongs to NCBI, but this code is not same as what we can see through the browser. Do you have any idea why??
Claire Huang
It is possible that there is some server side scripting going on which serves different content based on the browser it sees. Try setting the user agent string to mimic firefox or something. Try this:`reply->setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 (.NET CLR 3.5.30729)");`or something similar.
Evan Teran
The setHeader is a protected function in QNetworkReply so it cannot be called directly, but your answer let me know the reason and give me some direction, thanks :)(I should vote up but I don't have enough reputation XD)I'll try to set up the browser first to see if it works. there is one thing, this will only affect the html code we get, right? I mean, we don't need to have firefox and windows on our system? I need the cross-platform property.
Claire Huang
yea, so you can just inherit from the `QNetworkReply` class and call `setHeader` in it's constructor. You won't need firefox or anything like that installed, it only effects what browser it will claim to be :-).
Evan Teran