【问题标题】:How to set QNetworkReply properties to get correct NCBI pages?如何设置 QNetworkReply 属性以获取正确的 NCBI 页面?
【发布时间】:2011-02-04 06:56:45
【问题描述】:

我尝试使用 downloadURL 函数获取以下网址,如下所示:

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

但是数据不是我们通过浏览器可以看到的,现在我知道是因为需要一些正确的信息(例如浏览器类型)。如何知道需要设置哪些信息,如何设置? (通过 setHeader 函数或其他方式??)

在VC++中,我们可以使用CInternetSession和CHttpConnection对象来获取正确的数据,而不需要设置任何其他细节信息,Qt或者其他跨平台C++网络库有没有类似的方法?? (是的,我需要跨平台属性。)

QNetworkReply::NetworkError downloadURL(const QUrl &url, QByteArray &data) {
    QNetworkAccessManager manager;
    QNetworkRequest request(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader ,"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)");
    QNetworkReply *reply = manager.get(request);

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


    QVariant statusCodeV = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    QUrl redirectTo = statusCodeV.toUrl();

    if (!redirectTo.isEmpty())
    {
        if (redirectTo.host().isEmpty())
        {
            const QByteArray newaddr = ("http://"+url.host()+redirectTo.encodedPath()).toAscii();
            redirectTo.setEncodedUrl(newaddr);
            redirectTo.setHost(url.host());
        }
        return (downloadURL(redirectTo, data));
    }

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

通过VC,我们可以这样做,那么正确的数据在CHttpFile中。

CString downloadURL (CString sGetFromURL)
{
    // create an internet session 
    CInternetSession csiSession;

    int pos;
    BOOL neof;

    // parse URL to get server/object/port 

    DWORD dwServiceType;
    CString sServerName;
    CString sObject;
    INTERNET_PORT nPort;
    CHttpConnection* pHTTPServer = NULL; 
    CHttpFile* pFile = NULL;


        AfxParseURL ( sGetFromURL, dwServiceType, sServerName, sObject, nPort );

        // open HTTP connection 
        pHTTPServer = csiSession.GetHttpConnection ( sServerName, nPort ); 

        // get HTTP object 
        pFile = pHTTPServer->OpenRequest ( CHttpConnection::HTTP_VERB_GET, sObject, NULL, 1, NULL, NULL, INTERNET_FLAG_RELOAD ); 

        pFile->SendRequest();

}

【问题讨论】:

    标签: c++ qt qt4 qnetworkaccessmanager ncbi


    【解决方案1】:

    您设置了错误的 Content-Type 标头。您提供的值更适合 User-Agent 标头

    【讨论】:

    • 谢谢 :) 我找到了一些其他的 Url 来获取数据,但它仍然有助于解决这个问题
    【解决方案2】:

    关闭,但您没有设置正确的标题。你需要做的:

    request.setRawHeader("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)" );
    

    【讨论】:

    • 谢谢 :) 我找到了一些其他的 url 来获取数据。但是,很高兴知道我的代码中的问题出在哪里。下次我会知道怎么做的^^
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 2013-12-28
    • 2016-01-02
    • 1970-01-01
    相关资源
    最近更新 更多