qt http头文件
时间: 2023-12-14 19:05:08 浏览: 86
以下是Qt中使用HTTP网络请求的头文件和相关代码示例:
```cpp
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QJsonParseError>
#include <QJsonObject>
#include <QTime>
// 创建QNetworkAccessManager对象
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
// 创建QNetworkRequest对象
QNetworkRequest request;
request.setUrl(QUrl("http://example.com"));
// 发送GET请求
QNetworkReply *reply = manager->get(request);
// 处理响应
connect(reply, &QNetworkReply::finished, [=]() {
if (reply->error() == QNetworkReply::NoError) {
QByteArray data = reply->readAll();
QJsonParseError error;
QJsonObject json = QJsonDocument::fromJson(data, &error).object();
if (error.error == QJsonParseError::NoError) {
// 解析JSON数据
QString time = json.value("time").toString();
QTime t = QTime::fromString(time, "hh:mm:ss");
qDebug() << "Current time is:" << t.toString();
}
} else {
qDebug() << "Error:" << reply->errorString();
}
reply->deleteLater();
});
```
阅读全文