2

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

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

此网址将重定向到

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

我尝试通过以下方式做到这一点,但我什么也得不到。它适用于某些网页,例如 www.google.com,但不适用于此 NCBI 页面。有什么办法可以得到这个页面??

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

}
4

1 回答 1

3

该页面似乎有一个重定向。

来自 4.6 的 Qt 文档:

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

于 2010-04-04T00:40:00.620 回答