Domanda

I cercare di ottenere questa seguente URL utilizzando la funzione downloadURL nel seguente modo:

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

Ma i dati non è come quello che si può vedere attraverso il browser, ora so che è perché sono necessarie alcune informazioni corrette (come ad esempio il tipo di browser). Come faccio a sapere che tipo di informazioni che ho bisogno di set, e come posso impostare? (Con la funzione SetHeader o qualche altro modo ??)

In VC ++, possiamo usare CInternetSession e CHttpConnection oggetto per ottenere i dati corretti senza impostare qualsiasi altra informazione particolare, c'è qualche modo simile in Qt o altro cross-platform C ++ lib rete ?? (Sì, ho bisogno la proprietà del cross-platform.)

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

Per VC, possiamo solo fare questo, allora i dati corretti è nel 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();

}
È stato utile?

Soluzione

Vicino, ma non state impostando l'intestazione corretta. Hai bisogno di fare:

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

Altri suggerimenti

È impostato male header Content-Type. Il valore fornito si adatta più intestazione User-Agent

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top