如何使用Qt获取重定向页面的html代码? [英] How can I use Qt to get html code of the redirected page?

查看:2185
本文介绍了如何使用Qt获取重定向页面的html代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Qt从以下网址下载html代码:

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=核苷酸& cmd =搜索& term = AB100362

此网址将重定向到

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

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

我试着这样做,但我不能得到任何东西。
它适用于某些网页,如www.google.com,但不适用于此NCBI页面。是否有任何方式获取此页面?

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";
        }

}


推荐答案

该网页似乎有重新导向。

That page appears to have a redirect.

从Qt docs for 4.6:

From the Qt docs for 4.6:


注意:当HTTP协议返回
重定向时,不会报告错误。
您可以使用
QNetworkRequest :: RedirectionTargetAttribute
属性检查是否存在重定向

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.

这篇关于如何使用Qt获取重定向页面的html代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆