Qt多部分发布问题
我需要帮助发送多部分发布请求。主要问题是服务器无法识别我的请求的帖子正文部分。服务器端在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在设置“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")
从 Qt 4.8 开始,出现了 QHttpMultiPart 类来实现这一点。
Since Qt 4.8 there appeared QHttpMultiPart class which does the trick.