QNetworkAccessManager - 如何发送MultiPart“PATCH”请求

时间:2016-02-28 14:35:09

标签: qt multipart qnetworkaccessmanager qtnetwork qnetworkrequest

这个问题是补充 QNetworkAccessManager - How to send “PATCH” request

QNetworkAccessManager没有方法

sendCustomRequest(const QNetworkRequest & request, const QByteArray & verb, QHttpMultiPart * multiPart)

我坚持使用Qt 4.8-bb10。我该怎么办?

1 个答案:

答案 0 :(得分:0)

我认为您可以通过将额外部分放在数据中来自行构建多部分请求,如下所示。

很抱歉,但我无法测试,所以这只是一个粗略的想法。

QUrl url("http://data.mybusiness.com/patches");
QNetworkRequest request(url);
QString boundary("------------------------------------asdfyiuqwer762345");
request.setRawHeader("Content-Type", QByteArray("multipart/form-data; boundary=").append(boundary));

QByteArray data;
data.append("--" + boundary + "\r\n");
data.append("Content-Disposition: form-data; name=\"City\"\r\n");
data.append("\r\n");
data.append("Paris\r\n");
data.append("--" + boundary + "\r\n");

data.append("Content-Disposition: form-data; name=\"Country\"\r\n");
data.append("\r\n");
data.append("Canada\r\n");
data.append("--" + boundary + "--\r\n");
/* Final boundary has extra -- at end */

QBuffer * pBuffer = new QBuffer(pNetworkAccessManager);
pBuffer->setData(data);

QNetworkReply * pReply = pNetworkAccessManager->sendCustomRequest(request, "PATCH", pBuffer);