I'm starting with qt/c++ and on my project I need to make a json post/get/put but I'm trying to do a simple get request but I have an error: qt.network.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed
I don't know why, I tried some methods but all put the same message. I attach the code that I use.
mainwindow.cpp:
void MainWindow::replyFini(QNetworkReply* reply)
{
QString answer = QString::fromUtf8(reply->readAll());
qDebug() << "answer------------>"<<answer;
}
void MainWindow::on_btn_login_clicked()
{
QNetworkRequest request(QUrl("https://httpbin.org/get"));
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFini(QNetworkReply*)));
manager->get(request);
}
mainwindow.h:
#include <QtCore>
#include <QNetworkAccessManager>
#include <QNetworkReply>
On Windows if you install Qt via online installer, you can select OpenSSL Toolkit as an optional component. This should provide you with the version definitely compatible with your Qt.
If you haven't checked it during installation, you can rerun C:\Qt\MaintenanceTool.exe
and select Add or remove components. OpenSSL Toolkit is located under the Developer and Designer Tools section, it is the last entry.
However, there's a note in the sidebar:
Qt installer doesn't set up OpenSSL environment. User needs to define the path and environment variables.
So once installed, you should add the directory with the DLLs to the PATH environment variable or place the needed DLLs near the .exe
file (copy them to the debug
and release
directories of your project).
The directory is located at C:\Qt\Tools\OpenSSL\Win_x64\bin
(or Win_x86
if you need 32-bit version). The DLLs are named libssl-1_1-x64.dll
and libcrypto-1_1-x64.dll
in my case.
But this wasn't enough to deploy it to another machine, after copying it all I got the same error again. The reason came out to be that OpenSSL DLLs depend on another MSVC runtime version than the Qt app itself, and these errors go unreported. Open these DLLs in Dependency Walker to find out what's missing. In my case it was MSVCR100.dll
, so I had to install MSVC 2010 Redistributable x64 (x86 is here).
The SSL libraries are not found. The error can be read out in the Qt Src. Either deploy them with your application or install OpenSSL on your machine.
Also, just a small hint:
void MainWindow::replyFini(QNetworkReply* reply)
{
QString answer = QString::fromUtf8(reply->readAll());
qDebug() << "answer------------>"<<answer;
}
You should call reply->deleteLater();
void MainWindow::on_btn_login_clicked()
{
QNetworkRequest request(QUrl("https://httpbin.org/get"));
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFini(QNetworkReply*)));
manager->get(request);
}
Add connect(manager, &QNetworkAccessManager::finished, manager, &QNetworkAccessManager::deleteLater);
to avoid a memory leak on each click.
What worked for me was to download from https://slproweb.com/products/Win32OpenSSL.html the OpenSSL light version (for my compiler) and copy the libcrypto and libssl DLLs from c:\Program Files\OpenSSL\ to my program's .exe folder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With