TLS initialization failed on GET Request

Viewed 21320

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>
4 Answers

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.

I ran into this same issue while trying to integrate the arcGIS geographic mapping system into qt creator and was only able to resolve it after I learned that different releases of Qt require different releases of openSSL. I was working with Qt Creator 5.15.2 and was encountering the error because I had installed the newest version of openSSL. Using this line of code

qDebug() << QSslSocket::supportsSsl() << SslSocket::sslLibraryBuildVersionString() << QSslSocket::sslLibraryVersionString();

I was able to identify the version of openSSL Qt was looking for (don't forget #include QSslSocket for this to work). Once I installed the correct version (1.1.1q) everything worked perfectly.

Here is a link to download different releases of openSSL. It is also still necessary to add the needed built-in wrapper using the qt maintenance tool -> developer tools -> openSSL.

Finally a good way to check which, if any, version of openSSL is currently on your computer (at least with windows) is by simply typing this command into command prompt.

$ openSSL version

This should result in either the current version of openSSL your OS is running, or will inform you that the command isn't recognized.

Related