概述
使用 network 头文件
1.初始化客户端
QNetworkAccessManager *m_manager;
m_manager = new QNetworkAccessManager(this);
connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
void Widget::replyFinished(QNetworkReply *reply)
{ //服务器返回数据
qDebug() <<"接受数据"<< QString::fromUtf8(reply->readAll());
}
2.准备数据 开始发送
(1)数据 key =deviceNum value = 2
QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
QHttpPart textPart;
textPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name="deviceNum""));
textPart.setBody("2");
multiPart->append(textPart); //
(2) // 数据 key =file value = D://1.pdf 的文件
QString path = QString("D://1.pdf");
QHttpPart Part;
Part.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("text/plain"));
Part.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name="file"; filename="1.pdf""));
QFile *File = new QFile(path);
File->open(QIODevice::ReadOnly);
Part.setBodyDevice(File);
File->setParent(multiPart);
multiPart->append(Part);
(3)//数据 key =file value = D://1.PNG 的图片
QString path = QString("D://1.png");
QHttpPart Part;
Part.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("mage/jpeg"));
Part.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name="file"; filename="1.png""));
QFile *File = new QFile(path);
File->open(QIODevice::ReadOnly);
Part.setBodyDevice(File);
File->setParent(multiPart);
multiPart->append(Part);
(4)//数据 key =file value = D://1.PNG 的图片 使用QImage filename的格式可以不一样
QString path = QString("D://1.png");
QImage img=QImage(path);
QHttpPart imagePart;
imagePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("image/jpeg"));
imagePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name="file"; filename="背景.jpg""));
QByteArray ba;
QBuffer buffer(&ba);
buffer.open(QIODevice::WriteOnly);
img.save(&buffer, "PNG"); //通一张图片 用jpg/bmp 都是可以上传 感觉对图片格式不严格
imagePart.setBody(ba);
multiPart->append(imagePart);
/数据准备好 开始发送//
QUrl url=QUrl("http://192.168.1.120/meeting/public/api/common/userSign");
QNetworkRequest request;
request.setUrl(url);
QNetworkReply * uploadFileReply= m_manager->post(request, multiPart);
multiPart->setParent(uploadFileReply);
最后
以上就是超级煎蛋为你收集整理的Qt 发送 http 数据到服务端的全部内容,希望文章能够帮你解决Qt 发送 http 数据到服务端所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复