Haciendo un HTTP GET bajo Qt

I have kind of a n00b problem, I can't seem to make HTTP GET requests from my Qt Code...

Here is the code supposed to work:

void MainWindow::requestShowPage(){
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(requestReceived(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http://google.com")));
}

void MainWindow::requestReceived(QNetworkReply* reply){
    QString replyText;
    replyText.fromAscii(reply->readAll());

    ui->txt_debug->appendPlainText(replyText);
}

But the problem is that this just doesn't work: In requestReceived(QNetworkReply* reply), replyText seems empty, reply->error() devoluciones 0 y reply->errorString() returns "Unknown Error". I don't really know what to do right now...

¿Alguna idea?

preguntado el 28 de agosto de 11 a las 03:08

2 Respuestas

There is obviously a redirection, which is not considered as an error.
You should run a new request with the redirection url provided in the reply attributes until you get the real page:

void MainWindow::requestReceived(QNetworkReply *reply)
{
    reply->deleteLater();

    if(reply->error() == QNetworkReply::NoError) {
        // Get the http status code
        int v = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
        if (v >= 200 && v < 300) // Success
        {
             // Here we got the final reply 
            QString replyText = reply->readAll();
            ui->txt_debug->appendPlainText(replyText);
        } 
        else if (v >= 300 && v < 400) // Redirection
        {
            // Get the redirection url
            QUrl newUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
            // Because the redirection url can be relative, 
            // we have to use the previous one to resolve it 
            newUrl = reply->url().resolved(newUrl);

            QNetworkAccessManager *manager = reply->manager();
            QNetworkRequest redirection(newUrl);
            QNetworkReply *newReply = manager->get(redirection);

            return; // to keep the manager for the next request
        } 
    } 
    else 
    {
        // Error
        ui->txt_debug->appendPlainText(reply->errorString());
    }

    reply->manager()->deleteLater();
}

You should also record where you are redirected or count the number of redirections, to avoid never ending loops.

Respondido 06 Oct 17, 15:10

Thank you so much! Your solution works like a charm! I would have never tough of it... - m6a-uds

@alexisdm Hi, I was having a similar issue to this, except my URL that is redirected also has post data, is there anything similar to this code that would work for that? - thnkwthprtls

If reply->error() = 0, it means the request was successful. In fact, your code seems right to me, and the only thing I would do differently is to read the data. Try with this:

QByteArray rawData = reply->readAll();
QString textData(rawData);
qDebug() << textData;

Respondido 28 ago 11, 07:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.