-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttppost.cpp
40 lines (35 loc) · 1.1 KB
/
httppost.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "httppost.h"
httppost::httppost(QObject *parent) : QObject(parent)
{
}
bool httppost::postSyn( QString url, QMap<QString, QString> headerdata, QByteArray requestData, QByteArray &replydata)
{
//发送请求的对象
QNetworkAccessManager manager;
//请求对象
QNetworkRequest request;
request.setUrl(url);
QMapIterator<QString, QString> it(headerdata); //迭代器
while (it.hasNext()) //遍历Map
{
it.next();
request.setRawHeader(it.key().toLatin1(),it.value().toLatin1());
}
QNetworkReply *reply = manager.post(request, requestData) ;
QEventLoop l;
//一旦服务器返回,reply会发出信号
connect(reply, &QNetworkReply::finished, &l, &QEventLoop::quit);
l.exec();
//死循环,reply发出信号, 结束循环
if (reply != nullptr && reply->error() == QNetworkReply::NoError)
{
replydata = reply->readAll(); //读取服务 器返回的数据
qDebug() << replydata;
return true;
}
else
{
qDebug() << "请求失败";
return false;
}
}