我尝试使用多部分表单提交,以便将文件上传到Web服务器。我从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.问题绝对不是接收数据的脚本,因为我已将其简化为简单地返回" Hello World"无论请求是什么。
答案 0 :(得分:3)
似乎Qt(版本5.5)对边界做错了。我能够通过将边界设置为我自己选择的字符串并设置指定边界的标题来使其工作。
我添加了这两行:
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()));