qt怎么接收post数据_QT实现同步的GET和POST请求

有时候需要等待得到返回信息才能继续做下一步。这时候用异步就超级难用了,就需要用同步/阻塞的方式了。

get

QByteArray get(const QString &str_url){

const QUrl url = QUrl::fromUserInput(str_url);

QNetworkRequest qnr(url);

QNetworkAccessManager qnam;

QNetworkReply *reply = qnam.get(qnr);

QEventLoop eventloop;

QObject::connect(reply, &QNetworkReply::finished, &eventloop, &QEventLoop::quit);

eventloop.exec(QEventLoop::ExcludeUserInputEvents);

QByteArray reply_data = reply->readAll();

reply->deleteLater();

reply = nullptr;

return reply_data;

}

POST

QByteArray post(const QString &str_url, const QString &data){

const QUrl url = QUrl::fromUserInput(str_url);

QNetworkRequest qnr(url);

QNetworkAccessManager qnam;

QNetworkReply *reply = qnam.put(qnr, data.toUtf8());

QEventLoop eventloop;

QObject::connect(reply, &QNetworkReply::finished, &eventloop, &QEventLoop::quit);

eventloop.exec(QEventLoop::ExcludeUserInputEvents);

QByteArray reply_data = reply->readAll();

reply->deleteLater();

reply = nullptr;

return reply_data;

}

你可能感兴趣的:(qt怎么接收post数据)