QNetworkAccessManager sends GET two times

筅森魡賤 提交于 2019-12-23 21:37:31

问题


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.


回答1:


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
   }


来源:https://stackoverflow.com/questions/13803476/qnetworkaccessmanager-sends-get-two-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!