Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TLS initialization failed on GET Request

Tags:

c++

json

qt

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>
like image 629
ikan nedes Avatar asked Dec 16 '18 19:12

ikan nedes


3 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).

like image 160
user Avatar answered Oct 18 '22 05:10

user


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.

like image 8
user3606329 Avatar answered Oct 18 '22 06:10

user3606329


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.

like image 4
Adriel Jr Avatar answered Oct 18 '22 06:10

Adriel Jr