QNetworkAccessManager 发送 GET 两次 [英] QNetworkAccessManager sends GET two times

查看:72
本文介绍了QNetworkAccessManager 发送 GET 两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些类可以干扰 HTTP 服务器.这是有意义的代码部分:

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;
}

当我调用 run() 方法时,有时(不是每次)是两个相同的 GET 请求(我用 tcpdump 看了).qDebug() 执行 1 次.
我的代码中有错误吗?我看不到任何可能的解释.

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.

更新:经过一些 tcpdump 输出研究.
在第二次请求之后,它发送带有 RST 标志的数据包作为对 FIN 的回答.但是我仍然看不出触发问题的 TCP 流和不触发问题的 TCP 流没有区别.
F.e.这是wireshark的输出.Stream 8 进展顺利.Stream 11流 12.
我被这个困住了.也许这是服务器大小的一些协议错误,我不确定.或者可能是 QNetworkAccessManager 中的错误.

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.

推荐答案

您是否尝试过在不使用本地范围内的 QEventLoop 的情况下重写代码以使其更加异步?您的代码对我来说看起来不错,但是您可能会遇到一些奇怪的 QT 错误,因为它会在本地范围内排队处理请求和使用 QEventLoop 时遇到这些错误.我通常通过以下方式使用 QNetworkAccessManager 来发送 GET 和 POST 请求:

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
   }

这篇关于QNetworkAccessManager 发送 GET 两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆