在Qt中发布图片时如何添加请求正文?

时间:2019-01-05 05:29:39

标签: qt http post

我想将图像文件发布到服务器时感到困惑。文章清楚,我可以只使用QHttpMultiPart,并且工作正常。但是这一次我想添加一些参数,服务器却没有。

我搜索了几天,但未找到解决方案。 代码如下:

QUrl url("http://192.168.1.211/v1");
QUrlQuery query;
//following params are  what i want to add
query.addQueryItem("app_name", "pc");
query.addQueryItem("ip", "192.168.1.110");
query.addQueryItem("dev_os","android");
query.addQueryItem("dev_os_ver","11.4.1");
query.addQueryItem("dev_model",testEquip);
query.addQueryItem("latitude","23.137466");
query.addQueryItem("longitude","113.352425");
query.addQueryItem("user_id","1");
query.addQueryItem("nickname","nick");
query.addQueryItem("sex","1");
query.addQueryItem("headimgurl","http://some.com/");
url.setQuery(query);

QNetworkRequest req;
req.setUrl(url);
req.setRawHeader("X-Api-Key","JDZBNH3HDFI823AISDU9SF32IRJ");

QHttpMultiPart *multiPart=new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader,QVariant("form-data; name=\"image\"; filename=\""+filename+"\""));
QFile *file=new QFile(path);
file->open(QIODevice::ReadOnly);
imagePart.setBodyDevice(file);
file->setParent(multiPart);
multiPart->append(imagePart);

QNetworkAccessManager *manager=new QNetworkAccessManager();
QNetworkReply *reply=manager->post(req,multiPart);
multiPart->setParent(reply);

响应代码显示未访问任何参数。那么添加这些参数的正确方法是什么?

0 个答案:

没有答案