背景:
现在公司项目需要做一个检测更新的功能,通过访问后台接口,判断是否需要更新。
后台接口是 https 协议的,接口需要post请求,需要带参数,来判断是哪个软件需要做检测更新的操作。
客户端软件使用Qt写的
重点 & 注意事项:
1.https协议,需要用到 ssl,如果跑起来报错,基本都是 ssl 的问题,我的Qt版本是 5.12.10,需要安装:Win32OpenSSL-1_1_1q.msi,然后找到两个动态库(libcrypto-1_1.dll 、libssl-1_1.dll)放在exe同级目录
2.Win32OpenSSL 官方下载地址:https://slproweb.com/products/Win32OpenSSL.html
3.Win32OpenSSL 我自己下载的安装包,下载地址:https://www.aliyundrive.com/s/FqTfEDN9i83
思路清晰,代码简单,直接上代码:
.h
#pragma once #include <QtWidgets/QDialog> #include "ui_QtWidgetsApplication1.h" #include "qmessagebox.h" #include <QNetworkAccessManager> #include <QNetworkRequest> #include <QNetworkReply> class QtWidgetsApplication1 : public QDialog { Q_OBJECT public: QtWidgetsApplication1(QWidget *parent = nullptr); ~QtWidgetsApplication1(); QNetworkAccessManager* m_http; QNetworkReply* m_res; public slots: void test(); void testFinished(); private: Ui::QtWidgetsApplication1Class ui; };
.cpp
#include "QtWidgetsApplication1.h" QtWidgetsApplication1::QtWidgetsApplication1(QWidget *parent) : QDialog(parent) { ui.setupUi(this); connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(test())); } QtWidgetsApplication1::~QtWidgetsApplication1() {} void QtWidgetsApplication1::testFinished() { if (m_res->error() == QNetworkReply::NoError) { QString ba = m_res->readAll(); // 自行解析接口返回数据 QMessageBox::warning(this, "123", "345"); } else { QMessageBox::warning(this, "123", m_res->errorString()); } } void QtWidgetsApplication1::test() { m_http = new QNetworkAccessManager(); //设置头信息 QNetworkRequest m_url; m_url.setUrl(QUrl("https://pmp.eloam.net/api/ota/findFadVersion")); m_url.setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); QSslConfiguration m_sslConfig = QSslConfiguration::defaultConfiguration(); m_sslConfig.setPeerVerifyMode(QSslSocket::VerifyNone); m_sslConfig.setProtocol(QSsl::TlsV1_2); m_url.setSslConfiguration(m_sslConfig); char cByte[1024] = "{\"as\":\"123456\", \"ks\": \"123456\", \"productCode\": \"HSPS\", \"version\": \"V1.2.3\"}"; QByteArray bate(cByte); //发送数据 m_res = m_http->post(m_url, bate); connect(m_res, &QNetworkReply::finished, this, &QtWidgetsApplication1::testFinished); }
标签:QtWidgetsApplication1,Qt,url,res,void,https,post,include From: https://www.cnblogs.com/shiyixirui/p/16821449.html