QHttpMultiPart POST returns 错误 500

QHttpMultiPart POST returns error 500

我正在试验多部分表单提交,目的是将文件上传到网络服务器。我从 QHttpMultiPart 文档中的示例中改编了以下代码:

QHttpMultiPart * pMultiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);

QHttpPart textPart1;
textPart1.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"Sport1\"");
textPart1.setBody("Dodgeball");
pMultiPart->append(textPart1);

QHttpPart textPart2;
textPart2.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"Sport2\"");
textPart2.setBody("Kickball");
pMultiPart->append(textPart2);

QNetworkRequest request(myUrl);
QNetworkReply * pReply = m_pNetworkManager->post(request, pMultiPart);
pMultiPart->setParent(pReply);
connect(pReply, SIGNAL(finished()), this, SLOT(replyFinished()));

服务器一直拒绝提交并显示错误 500。问题绝对不是接收数据的脚本,因为无论请求是什么,我都将其简化为 return "Hello World"。

Qt(5.5 版)似乎在边界上做错了。我能够通过将边界设置为我自己选择的字符串和设置指定边界的 header 来使其工作。

我添加了这两行:

pMultiPart->setBoundary("---------------------jasglfuyqwreltjaslgjlkdaghflsdgh");
...
request.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary=" + pMultiPart->boundary());

全部都在这里:

QHttpMultiPart * pMultiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
pMultiPart->setBoundary("---------------------jasglfuyqwreltjaslgjlkdaghflsdgh");

QHttpPart textPart1;
textPart1.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"Sport1\"");
textPart1.setBody("Dodgeball");
pMultiPart->append(textPart1);

QHttpPart textPart2;
textPart2.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"Sport2\"");
textPart2.setBody("Kickball");
pMultiPart->append(textPart2);

QNetworkRequest request(myUrl);
request.setHeader(QNetworkRequest::ContentTypeHeader, "multipart/form-data; boundary=" + pMultiPart->boundary());
QNetworkReply * pReply = m_pNetworkManager->post(request, pMultiPart);
pMultiPart->setParent(pReply);
connect(pReply, SIGNAL(finished()), this, SLOT(replyFinished()));