[Qt-interest] QT 4.6: Strange behavior while using QNAM for multiple HTTP requests

Mandeep Sandhu mandeepsandhu.chd at gmail.com
Mon May 3 07:49:20 CEST 2010


Hi All,

I'm experiencing some strange behavior while using
QNetworkAccessManager (QNAM) for fetching data from multiple URL's in
a loop. The finished signal of QNAM is emitted TWICE for each URL,
once with full data and next with 0 bytes. I'm not sure if my usage is
correct or not.

Here's the code snippet which actually starts the download in a loop.
I have placed 2 files, test1.jpg and test2.jpg in webserver's (nginx)
root.

<snip>
void download() {
    for (int i=1; i<3; i++) {
        QString urlStr = QString("http://localhost/test%1.jpg").arg(i);
        qDebug() << urlStr;

        QNetworkRequest req;
        req.setUrl(QUrl(urlStr));
        qDebug() << "Pipelining allowed -"
                 <<
req.attribute(QNetworkRequest::HttpPipeliningAllowedAttribute).toBool();

        mgr->get(req);
        connect(mgr, SIGNAL(finished(QNetworkReply*)),
                this, SLOT(getData(QNetworkReply*)));
    }
}

void getData(QNetworkReply* reply) {
    qDebug() << "Reply for req" << reply->request().url();
    qDebug() << "Status code"
             <<
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();

    if (reply->error() != QNetworkReply::NoError) {
        qDebug() << "Error fetching data from" << reply->url();
        return;
    }

    qDebug() << "Bytes available" << reply->bytesAvailable();

    QByteArray bytes = reply->readAll();
    qDebug() << "Rx'ed" << bytes.size();
    reply->deleteLater();
}
</snip>

On the webserver I see only 2 requests landing, i.e for test1.jpg and
test2.jpg. However, the getData() slot is getting called twice!
readAll() is getting me the complete file the first time and in the
next callback it's 0.

Also, if I run the loop only once (i.e put only a single req for say
test1.jpg, then the getData slot gets called only once!).

The status code returned is 200 always.

Also, the request object is showing that the HTTP Pipelining is NOT
supported by QNAM. Why is this so? Is there anyway I can enable HTTP
Pipelining? I'm running all this on Ubuntu 9.10 (not upgraded to lynx
yet! :)).

Any suggestions?

(The full code is attached)

Thanks,
-mandeep
-------------- next part --------------
A non-text attachment was scrubbed...
Name: main.cpp.zip
Type: application/zip
Size: 828 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100503/876b3aa3/attachment.zip 


More information about the Qt-interest-old mailing list