一、背景
需要为当前项目的QT版本升级,从版本QT5.7.0升级到QT5.12.9,发现当前项目用QT5.12.9编译后,发现检查版本更新接口报错,用老版没有报错,难道是模块的问题?,用新版QTCreator编写一个简单demo测试,发现基本使用都正常,但是网络请求异常
二、测试例子
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QNetworkAccessManager>
#include <QNetworkReply>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_pushButton_clicked();
void replyFinished(QNetworkReply *reply);
private:
Ui::MainWindow *ui;
QNetworkAccessManager m_netManager;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->label->setFixedSize(100,100);
ui->label->setScaledContents(true);
qDebug() << "OpenSSL支持情况:" << QSslSocket::supportsSsl();
qDebug()<<"OpenSSL version="<<QSslSocket::sslLibraryBuildVersionString();
connect(&m_netManager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
ui->pushButton->setEnabled(false);
QUrl url;
url = QUrl(ui->lineEdit->text());
QNetworkRequest request;
request.setUrl(url);
m_netManager.get(request);
}
void MainWindow::replyFinished(QNetworkReply *reply)
{
ui->pushButton->setEnabled(true);
if(reply->error() == QNetworkReply::NoError)
{
QByteArray bytes = reply->readAll();
QPixmap pixmap;
pixmap.loadFromData(bytes);
ui->label->setPixmap(pixmap);
QFile file("E:/test.png");
if (file.open(QIODevice::Append))
{
file.write(bytes);
}
file.close();
}
else
{
qDebug() << "Error = "<<reply->error();
}
}
三、问题:
qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed
Error = QNetworkReply::ProtocolUnknownError
QT官网给出的release note里写的:
We wanted to update now as the earlier version of OpenSSL runs out of support at the end of the year . Unfortunately OpenSSL 1.1 is binary incompatible with 1.0, so users need to switch to the new one and repackage their applications.One important functionality enabled by OpenSSL 1.1 is TLS 1.3 bringing significant cryptography and speed improvements.
Qt自带的老版本OpenSSL1.0.x在今年(2019)就要结束LTS支持了,从本版本开始(5.12.4)Qt将启用1.1.x,新版本带来的TLS1.3,以前的libeay32.dll
和ssleay32.dll
变成了libcrypto-1_1.dll
和libssl-1_1.dll
OpenSSL官网说明
The latest stable version is the 1.1.1 series. This is also our Long Term Support (LTS) version, supported until 11th September 2023. Our previous LTS version (1.0.2 series) will continue to be supported until 31st December 2019 (security fixes only during the last year of support)
四、解决办法:
//查看当前OpenSSL支持情况
qDebug() << "OpenSSL支持情况:" << QSslSocket::supportsSsl();
//查看支持的版本
qDebug()<<"OpenSSL version="<<QSslSocket::sslLibraryBuildVersionString();
安装完QT5.12.4及其以上版本这个应该会显示false,因为我写博客的时候已经安装了,所以显示true
找到版本以后就是找响应的动态库了,找到这两个库,放到bin目录下,运行就行
1.用现成的专门的安装程序,安装完以后,将libcrypto-1_1.dll
和libssl-1_1.dll
拷贝到项目下,我一开始使用的就是这个方案。
下载地址:
http://slproweb.com/products/Win32OpenSSL.html
一路默认安装就行,安装完以后将下边这两个文件(就是默认安装路径下)放到自己工程的bin目录下就行,自己工程的网络请求就能正常使用了
2.自己编译OpenSSL,生成Debug/Release版本
参考链接:https://blog.csdn.net/qq_27415615/article/details/107794471