Qt多部分发布问题

发布于 2024-11-16 05:20:25 字数 1099 浏览 4 评论 0原文

我需要帮助发送多部分发布请求。主要问题是服务器无法识别我的请求的帖子正文部分。服务器端在 android 上运行良好,但在 Qt 和 NetworkAccessManager 上运行不佳。

这是代码:

这里我使 bodyPart 与请求一起发送(transferData 是带有一些文本的 QByteArray):

postBody.append("\r\n--"+ boundary + "\r\n");
postBody.append("Content-Disposition: form-data; name=\"auth\" \r\n");
postBody.append(" \r\n");
postBody.append(auth + " \r\n");
postBody.append("--"+ boundary + "\r\n");
postBody.append("Content-Disposition: form-data; name=\"upload-test-data\" \r\n");
postBody.append(" \r\n");
postBody.append(transferData + " \r\n");
postBody.append("--"+ boundary + "\r\n");
postBody.append("Content-Disposition: form-data; name=\"upload-checksum\" \r\n");
postBody.append(" \r\n");
postBody.append(checksum.toHex() + " \r\n");
postBody.append("\r\n--" + boundary + "--\r\n");

这是标题:

request.setRawHeader("User-Agent", "MyApp 1.0");
request.setRawHeader("Content-Type", "multipart/form-data; boundary="+boundary.toAscii());
request.setHeader(QNetworkRequest::ContentLengthHeader, QString::number(postBody.size()).toAscii());

I need help with sending multipart post request. Main problem is that server doesn't recognise my request's post body part. Server side is working fine with android, but not with Qt and NetworkAccessManager.

Here is the code:

Here I make bodyPart to send with request (transferData is a QByteArray with some text):

postBody.append("\r\n--"+ boundary + "\r\n");
postBody.append("Content-Disposition: form-data; name=\"auth\" \r\n");
postBody.append(" \r\n");
postBody.append(auth + " \r\n");
postBody.append("--"+ boundary + "\r\n");
postBody.append("Content-Disposition: form-data; name=\"upload-test-data\" \r\n");
postBody.append(" \r\n");
postBody.append(transferData + " \r\n");
postBody.append("--"+ boundary + "\r\n");
postBody.append("Content-Disposition: form-data; name=\"upload-checksum\" \r\n");
postBody.append(" \r\n");
postBody.append(checksum.toHex() + " \r\n");
postBody.append("\r\n--" + boundary + "--\r\n");

Here is headers:

request.setRawHeader("User-Agent", "MyApp 1.0");
request.setRawHeader("Content-Type", "multipart/form-data; boundary="+boundary.toAscii());
request.setHeader(QNetworkRequest::ContentLengthHeader, QString::number(postBody.size()).toAscii());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

才能让你更想念 2024-11-23 05:20:25

尝试在设置“Content-Type”后添加“Content-Length”标头。值应该类似于QString::number(postBody.length())。 (编辑:这没有解决问题)。

删除新行 (\r\n) 之前的额外空格有效。请将 postBody.append(" \r\n") 更改为 postBody.append("\r\n")

Try adding "Content-Length" header after setting "Content-Type". Value should be something like QString::number(postBody.length()). (Edited: This didn't fix the problem).

Removing the extra space before the new line (\r\n) worked. Kindly change postBody.append(" \r\n") to postBody.append("\r\n")

请你别敷衍 2024-11-23 05:20:25

从 Qt 4.8 开始,出现了 QHttpMultiPart 类来实现这一点。

Since Qt 4.8 there appeared QHttpMultiPart class which does the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文