tags:

views:

906

answers:

2

Hi,

I have written a program that uses qhttp to get a webpage. This works fine on Linux, but does not work on my Windows box (Vista). It appears that the qhttp done signal is never received.

The relevant code is:

    Window::Window()
{
    http = new QHttp(this);
    connect(http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
url = new QUrl("http://something.com/status.xml");
http->setHost(url->host(), url->port() != -1 ? url->port() : 80);
    if (!url->userName().isEmpty()) http->setUser(url->userName(), url->password());
}

void Window::retrievePage()
{ 
byteArray = new QByteArray;
result = new QBuffer(byteArray);
result->open(QIODevice::WriteOnly);

    httpRequestAborted = false;
    httpGetId = http->get(url->path(), result);
 }

 void Window::httpDone(bool error)
 {
     //Never gets here!
 }

Any help would be appriecated.

Matt

+1  A: 

This should not happen at all, i.e. QHttp works reliably both on Windows and Unix.

My advice is to check whether the serves gives proper response. This can be done e.g. by verifying that data transfer is fine. You can trace the status from QHttp's signal, e.g. dataReadProgress, requestStarted, requestFinished, and other related signals.

On the other hand, instead of using old QHttp, why not using the recommended QNetworkAccessManager instead? To get your feet wet quickly, check an example I posted to Qt Labs some time ago: image viewer with remote URL drag-and-drop support. It uses the said QNetworkAccessManager to grab the image from the dropped URL. Check the source-code, it is only 150 lines.

Ariya Hidayat
A: 

Rewritten as suggested by Ariya to use QNetworkAccessManager and looking at this example

This now works on Windows and Linux.

Window::Window()
{
   connect(&manager, SIGNAL(finished(QNetworkReply*)),
        this, SLOT(retrieveData(QNetworkReply*)));
}

void Window::retrieveMessage()
{
    manager.get(QNetworkRequest(QUrl("http://...")));
}

void Window::retrieveData(QNetworkReply *reply)
{
    QVariant statusCodeV = 
    reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);

    // "200 OK" received?
    if (statusCodeV.toInt()==200)
    {
        QByteArray bytes = reply->readAll();  // bytes
    }

    reply->deleteLater();
}
Matt Clarke