1、添加network模块
QT += core gui network
2、头文件添加
#include
#include
#include
#include//json
#include //https
#include
3、初始化HTTP的QNetworkAccessManager及建立连接信号槽
//声明 .h文件
private slots:
void finishedSlot(QNetworkReply *reply);
//初始化
m_accessManager = new QNetworkAccessManager();
QObject::connect(m_accessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
//接收函数
void MainWindow::finishedSlot(QNetworkReply *reply)
{
if (reply->error() == QNetworkReply::NoError)
{ = reply->readAll();
QString str(bytes);
QByteArray bytes
qDebug()<attribute(QNetworkRequest::HttpStatusCodeAttribute);
//statusCodeV是HTTP服务器的相应码,reply->error()是Qt定义的错误码,可以参考QT的文档
qDebug( "found error ....code: %d %d\n", statusCodeV.toInt(), (int)reply->error());
qDebug(qPrintable(reply->errorString()));
}
reply->deleteLater();
}
4、简单的get 实现代码
void MainWindow::on_pushButton_clicked()
{
qDebug("get");
QNetworkRequest request;
request.setUrl(QUrl("http://wthrcdn.etouch.cn/weather_mini?citykey=101020100"));
m_accessManager->get(request);
}
实验现象:"{\"data\":{\"yesterday\":{\"date\":\"2日星期日\",\"high\":\"高温 35℃\",\"fx\":\"南风\",\"low\":\"低温 28℃\",\"fl\":\"\",\"type\":\"多云\"},\"city\":\"上海\",\"forecast\":[{\"date\":\"3日星期一\",\"high\":\"高温 35℃\",\"fengli\":\"\",\"low\":\"低温 28℃\",\"fengxiang\":\"南风\",\"type\":\"多云\"},{\"date\":\"4日星期二\",\"high\":\"高温 32℃\",\"fengli\":\"\",\"low\":\"低温 28℃\",\"fengxiang\":\"东南风\",\"type\":\"小雨\"},{\"date\":\"5日星期三\",\"high\":\"高温 34℃\",\"fengli\":\"\",\"low\":\"低温 28℃\",\"fengxiang\":\"南风\",\"type\":\"小雨\"},{\"date\":\"6日星期四\",\"high\":\"高温 35℃\",\"fengli\":\"\",\"low\":\"低温 28℃\",\"fengxiang\":\"南风\",\"type\":\"阴\"},{\"date\":\"7日星期五\",\"high\":\"高温 35℃\",\"fengli\":\"\",\"low\":\"低温 28℃\",\"fengxiang\":\"西南风\",\"type\":\"阴\"}],\"ganmao\":\"感冒低发期,天气舒适,请注意多吃蔬菜水果,多喝水哦。\",\"wendu\":\"29\"},\"status\":1000,\"desc\":\"OK\"}"
因为url是一个天气你相关的接口,所以返回的是天气信息
5、简单POST请求,POTS 请求自动了参数
void MainWindow::on_pushButton_2_clicked()
{
QNetworkRequest request;
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json")); //设置数据类型
request.setUrl(QUrl("你自己的URL"));//注意填写自己相关URL
QJsonObject obj;
obj.insert("SoftVersion", "V1.0");
QByteArray byteArray = QJsonDocument(obj).toJson(QJsonDocument::Compact);
QString str(byteArray);
qDebug()<post(request,byteArray);
}
6、简单https请求
https 需要下载https库,因为需要Openssl支持,使用SSL进行认证,所以需要对应的库文件 libeay32.dll和ssleay32.dll。需要下载libeay32.dll 和 ssleay32.dll两个库文件,将这两个库文件拷贝到程序生成目录下(即生成exe的同级目录)或者拷贝到QtNetwork模块的库文件目录中,在 http://slproweb.com/products/Win32OpenSSL.html 中下载 Win32 OpenSSL进行安装,将bin目录下面的库拷贝出来放到生成程序exe同级目录下即可。
void MainWindow::on_pushButton_3_clicked()
{
QNetworkRequest request;
QSslConfiguration conf = request.sslConfiguration();
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
conf.setProtocol(QSsl::TlsV1SslV3);
request.setSslConfiguration(conf);
request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json")); //设置数据类型
request.setUrl(QUrl("自己的URL"));
QJsonObject obj;
obj.insert("SoftVersion", "V1.0");
QByteArray byteArray = QJsonDocument(obj).toJson(QJsonDocument::Compact);
// qDebug() << "user Info" << obj << endl;
QString str(byteArray);
// qDebug()<post(request,byteArray);
}