开发者

How can I use Qt to get html code of the redirected page?

开发者 https://www.devze.com 2022-12-25 07:56 出处:网络
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

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.addQuery开发者_运维知识库Item("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";
        }

}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号