QNetworkAccessManager 没有发送 POST 请求的数据部分

QNetworkAccessManager is not sending data part of POST request

向服务器发送 POST 数据时,Qt 应用程序看起来一切正常,但 HTTP 部分的数据部分未发送。在 Wireshark 中 POST 数据包是可见的正确 "Content-Length" 值,但整个 HTTP 段的大小只有大约 226 字节(与 POST 数据大小无关)。

我找不到数据部分未发送的原因。有什么想法吗?

应用程序在控制台中 运行。完整的来源如下。

project.pro:

QT += widgets
QT -= gui
QT += network

CONFIG += c++11

TARGET = POSTrequest
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

main.cpp:

#include <QObject>
#include <QApplication>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QNetworkRequest request(QUrl("http://www.server.com/index.php"));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

    QNetworkAccessManager manager;
    QNetworkReply *reply = manager.post(request, "a=aaaaaa");

    QObject::connect(reply, SIGNAL(finished()), &a, SLOT(quit()));

    return a.exec();
}

这对我在 OS X 和 Windows 上都有效:

#post-request-36549732.pro
QT = core network
CONFIG += console c++11
CONFIG -= app_bundle
TARGET = post-request-36549732
TEMPLATE = app
SOURCES += main.cpp
// main.cpp
#include <QtNetwork>

int main(int argc, char ** argv)
{
    QCoreApplication a{argc, argv};
    QNetworkAccessManager manager;
    QByteArray post{"a="};
    post.append(QByteArray{512, 'b'});
    QNetworkRequest req(QUrl("http://server/test.php"));
    req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");

    // Must be a queued connection, or else the multi-threaded manager might
    // win the race and signal quit before `a.exec()` starts running. In such
    // case, the `quit()` is a NOP. We don't want that.
    QObject::connect(&manager, &QNetworkAccessManager::finished, &a, [](QNetworkReply * reply){
       qDebug() << reply->errorString();
       qApp->quit();
    }, Qt::QueuedConnection);

    manager.post(req, post);
    return a.exec();
}

您可能错误地解释了 Wireshark 数据。确保您查看的是 HTTP 请求的 Reassembled TCP 方面。这是我的样子;服务器是 192.168.2.1: