1. 工程.pro配置
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c7f6d25cdce7375a3402edef9805b0d3.png)
2. 相关头文件
#include <QSslSocket>
#include <QSslConfiguration>
#include <QUrl>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkAccessManager>
public:
void startRequestPersignurl(QUrl url);
private:
QNetworkAccessManager *m_accessManager;
private slots:
void http_get_persign_url(QNetworkReply *reply);
void finishedSlot(QNetworkReply *reply);
3. https post相关QT代码实现
void MainWindow::startRequestPersignurl(QUrl url)
{
QJsonDocument doc ;
QByteArray post_Array;
QNetworkRequest request ;
QNetworkAccessManager *manager ;
QJsonObject create_device_json ;
QUrl baseUrl(url);
QSslConfiguration config = request.sslConfiguration();
config.setPeerVerifyMode(QSslSocket::VerifyNone);
config.setProtocol(QSsl::TlsV1SslV3);
request.setSslConfiguration(config);
request.setUrl(baseUrl);
manager = new QNetworkAccessManager(this);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
create_device_json.insert("access_id","ACMPGIYDEMLBMNUW65DQOJXW");
create_device_json.insert("access_key","MUZTSMLDMVSTGLJTGA3GILJUMYYTKLLCMVRWMLLBMM4DCY3EGI2TMOBXGUFA");
create_device_json.insert("machine_id","THE_ONE_MACHINE");
create_device_json.insert("machine_mac","10-50-56-C0-00-12");
create_device_json.insert("machine_type",111);
create_device_json.insert("nonce","5612513737");
doc.setObject(create_device_json);
post_Array = doc.toJson(QJsonDocument::Compact);
manager->post(request,post_Array);
connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(http_get_persign_url(QNetworkReply *)));
}
void MainWindow::http_get_persign_url(QNetworkReply *reply)
{
QVariant variant ;
QJsonParseError jsonError;
QByteArray rece = reply->readAll();
qDebug() << QString(rece);
ui->textBrowser->setText(QString(rece));
}
4. http put相关QT代码实现
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
m_accessManager = new QNetworkAccessManager(this);
QObject::connect(m_accessManager,SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
}
void MainWindow::on_pushButton_2_clicked()
{
QNetworkRequest request;
request.setUrl(QUrl("http://www.baidu.com/s?wd=name"));
m_accessManager->get(request);
}
void MainWindow::finishedSlot(QNetworkReply *reply)
{
if (reply->error() == QNetworkReply::NoError)
{
qDebug() << "receive data success\r\n";
QByteArray bytes = reply->readAll();
qDebug()<<bytes;
QString string = QString::fromUtf8(bytes);
ui->textBrowser->setText(string.toUtf8());
}
else
{
qDebug()<<"handle errors here";
QVariant statusCodeV = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
qDebug( "found error ....code: %d %d\n", statusCodeV.toInt(), (int)reply->error());
qDebug(qPrintable(reply->errorString()));
}
reply->deleteLater();
}