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

亡梦爱人 提交于 2019-12-07 19:26:45

问题


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

}

回答1:


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.



来源:https://stackoverflow.com/questions/2572985/how-can-i-use-qt-to-get-html-code-of-the-redirected-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!