Вопрос

I've got some class to interfere with HTTP-server. Here is meaningfull code parts:

const QString someClass::BASEURL = QString("http://127.0.0.1:8000/?");

someClass::someClass():
    manager(new QNetworkAccessManager(this))
{
}

QNetworkReply *someClass::run(QString request)
{
    qDebug() << request;
    QEventLoop loop;
    QObject::connect(manager, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
    QNetworkReply *res = manager->get(QNetworkRequest(QUrl(BASEURL + request)));
    loop.exec();
    return res;
}

When I call method run(), sometimes (not every time) the are two identical GET-requests (I looked with tcpdump). qDebug() executes 1 time.
Is there some error in my code? I can't see any possible explanation.

UPDATE: After some tcpdump ouptut research.
After second request it sends packet with RST flag as an answer to FIN. But I still can see no difference in TCP-streams that triggers the problem and that doesn't.
F.e. here is wireshark's output. Stream 8 went well. Stream 11 was duplicated with Stream 12.
I'm stuck with this. Maybe it's some protocol errors from server-size, I'm not sure. Or maybe it's a bug in QNetworkAccessManager.

Это было полезно?

Решение

Have you tried rewriting you code to be more asynchronous without using QEventLoop in a local scope? Your code looks good to me, but there might be some weird QT bug that you running into in the way it queues up requests for processing and using QEventLoop in the local scope. I usually use QNetworkAccessManager in the following manner to send GET and POST requests:

   void someClass::run(QString request)
   {
     qDebug() << request;
     QObject::connect(manager, SIGNAL(finished(QNetworkReply*)), this,  SLOT(on_request_complete(QNetworkReply*)));
     QNetworkReply *res = manager->get(QNetworkRequest(QUrl(BASEURL + request)));
   }

   void someClass::on_request_complete(QNetworkReply* response)
   {
      // Do stuff with your response here
   }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top