Qt网络请求
请求类 .h
//*****************Http_Pub
#ifndef HTTP_PUB_H
#define HTTP_PUB_H
#include
#include<QtNetwork/QNetworkReply>
#include
#include
#include//json
#include //https
#include <QtNetwork/QNetworkAccessManager>
class Http_Pub : public QObject
{
Q_OBJECT
public:
explicit Http_Pub(QObject *parent = nullptr);
public:
QString info;
QString token_head;
QString Message_Tools;
void http(QString user,QString pwd); //用于de登录
QByteArray http_info(QString method,QString url,QJsonObject data);//method post或get请求,url请求路由,data请求参数
public slots:
void finishedSlot(QNetworkReply *reply);
signals:
protected:
private:
QNetworkAccessManager *manager;
//QNetworkRequest *request;
private:
void new_obj();
void Json_Parse(QByteArray byte,QString data);
QByteArray http_array; //请求的信息
};
#endif // HTTP_PUB_H
请求类 .cpp
#include “http_pub.h”
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#pragma execution_character_set(“utf-8”)
Http_Pub::Http_Pub(QObject parent) : QObject(parent)
{
manager = new QNetworkAccessManager();
QObject::connect(manager, SIGNAL(finished(QNetworkReply)), this, SLOT(finishedSlot(QNetworkReply*)));
}
void Http_Pub::finishedSlot(QNetworkReply *reply)
{
if (reply->error() == QNetworkReply::NoError)
{QByteArray bytes = reply->readAll();
http_array = bytes;
QString str(bytes);
info = str;
//qDebug()<<str;
}
else
{
qDebug()<<“handle errors here”;
QVariant statusCodeV = reply->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();
}
void Http_Pub::new_obj()
{
//manager = new QNetworkAccessManager;
//request = new QNetworkRequest;
}
void Http_Pub::Json_Parse(QByteArray byte,QString data)
{
//解析JSON
QJsonParseError jsonError;
QJsonDocument jsonDoc = QJsonDocument::fromJson(byte, &jsonError);
if (jsonError.error != QJsonParseError::NoError) {
qDebug() << “Json文件解析错误。”;
}
if (jsonDoc.isObject())
{
qDebug() << “Json文件解析成功。”;
QJsonParseError jsonpe;
QJsonDocument json = QJsonDocument::fromJson(byte, &jsonpe);
QJsonObject data = json.object();
if(data[“response”].toString() == “ok”){
QString token = data[“data”].toObject()[“tbkt_token”].toString();
qDebug()<<token;
}else{
Message_Tools = data[“message”].toString();
qDebug()<<data[“message”].toString();
}
}
}
void Http_Pub::http(QString user,QString pwd)
{
// qDebug(“get”);
// QNetworkRequest request;
// request.setUrl(QUrl(“http://wthrcdn.etouch.cn/weather_mini?citykey=101020100”));
// manager->get(request);
//http
// QNetworkRequest request;
// request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(“application/json”)); //设置数据类型
// request.setUrl(QUrl(“XXXXX”));//注意填写自己相关URL
// QJsonObject obj;
// obj.insert(“SoftVersion”, “V1.0”);
// QByteArray byteArray = QJsonDocument(obj).toJson(QJsonDocument::Compact);
// QString str(byteArray);
// //qDebug()<<str;
// manager->post(request,byteArray);
//https
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("http://sapikz-beta.m.xueceping.cn/account/login"));
QJsonObject obj;
obj["phone"] = user;
obj["password"] = pwd;
QByteArray byteArray = QJsonDocument(obj).toJson(QJsonDocument::Compact);
// qDebug() << "user Info" << obj << endl;
QString str(byteArray);
// qDebug()<<str;
manager->post(request,byteArray);
}
QByteArray Http_Pub::http_info(QString method, QString url, QJsonObject data)
{
//请求方式method
if(method == “post”){
qDebug()<<“post请求”;
}
else if(method == "get"){
qDebug()<<"get请求";
QNetworkRequest request;
request.setUrl(QUrl(url));
manager->get(request);
Json_Parse(http_array,"data");
}
else if (method != "post" || method != "get") {
qDebug()<<"请求方式不正确,请重新确认请求方式";
}
}