post data in Qt5
QUrl url;
QByteArray postData;
url.setUrl("http://myurl....");
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
Qstring postKey = 'city';
QString postValue = 'Brisbane';
postData.append(postKey).append("=").append(postValue).append("&");
networkManager.post(request,postData);
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); QHttpPart textPart; textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"text\"")); textPart.setBody("my text"); QHttpPart imagePart; imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg")); imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"image\"")); QFile *file = new QFile("image.jpg"); file->open(QIODevice::ReadOnly); imagePart.setBodyDevice(file); file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart multiPart->append(textPart); multiPart->append(imagePart); QUrl url("http://my.server.tld"); QNetworkRequest request(url); QNetworkAccessManager manager; QNetworkReply *reply = manager.post(request, multiPart); multiPart->setParent(reply); // delete the multiPart with the reply // here connect signals etc.
Creating an HTTP network request in Qt
Overview
This code snippet demonstrates how to use QNetworkAccessManager for sending an HTTP request.
Note: In order to use this code, you need to have Qt installed on your platform.
Header
Source
Create QNetworkAccessManager and start listening for its finished signal.
HTTP GET request:
When the QNetworkAccessManager::finished signal is received, the HTTP request is completed.
本文介绍如何使用Qt进行HTTP网络请求,包括GET和POST方法。演示了如何设置请求头、发送表单数据及文件上传等操作。
1490

被折叠的 条评论
为什么被折叠?




In addition to a relevant list of preconditions, this article steps the reader through the relevant coding sections in a well thought out manner. The frequent line level coding documentation was useful for this reader (and better than his own). In the example code, the author demonstrates two alternate ways of using the reply read from the network, both of which will be helpful to developers that are new to QT.
--beakesland 21:26, 24 September 2009 (UTC)
This code goes against QT's own documentation
From the Article:
QT documentation (for signal finished()):
Suggest changing the code to reply.deleteLater();
pasihir 12:05, 16 November 2010 (UTC)
This code:
is wrong. Use
where XXX is the encoding of the data in the bytearray, ie fromUtf8(...)
It would be really helpful to have Qt Code double-checked by someone from Qt Dev, or at the very least allow editing of this kind of articles. Wrong information is worse than no information.
-apoenitz2 21:24, 19 May 2011
Patxitron - The C++ is wrong
In adition to the errors pointed by -apoenitz2, in the MyHttpEngine::finishedSlot since reply is a pointer to QNetworkReply the line
is wrong. It should be
Best regards
Patxitron
Patxitron (talk) 16:43, 26 September 2013 (EEST)
Hamishwillee - Thanks Patxitron
I've fixed up your error, as it is basic C++. Didn't fix the other stuff. This is a wiki, so if you're absolutely sure of yourself then you can change the article to fix it yourself.
Regards
Hamish