Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QNetworkRequest for https



  • I want to read json data from a remote server with an https get request. The server requires an Authorizsation header. I tried the following:

    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
        connect(manager, &QNetworkAccessManager::finished, this, &AccountsTableModel::replyFinished);
    QNetworkRequest request =  QNetworkRequest(QUrl("https://x.x.x.x:y/xyz"));
    request.setSslConfiguration(QSslConfiguration::defaultConfiguration());
    request.setRawHeader(QByteArray("Authorization"), QByteArray("bearer xyz"));
    request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    
    QNetworkReply *reply = manager->get(request);
    
    connect(reply, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error), this, &AccountsTableModel::replyError);
    connect(reply, &QNetworkReply::sslErrors, this, &AccountsTableModel::replySslError);
    
    void AccountsTableModel::replyError(QNetworkReply::NetworkError code)
    {
        qDebug() << "Error";
        qDebug() << (int) code;
    }
    
    void AccountsTableModel::replySslError(const QList<QSslError> &sslErrors)
    {
        for (int i = 0; i < sslErrors.size(); i++) {
            qDebug() << "Error " << i << sslErrors.at(i).errorString();
        }
    }
    

    When I run the code I get the following error messages:

    6
    "SSL handshake failed"
    "OpenSSL 1.0.2k-fips 26 Jan 2017"
    ""

    I'm using: Qt 5.12.3 on a linux system with openssl 1.0.2

    What is the correct way to use QNetwokrRequest with ssl?

    Did I set the Authorization header correctly?

    Where and how can I define the certificate?


  • Lifetime Qt Champion

    Hi,

    How did you install Qt ?
    Since you're on a linux distribution, and before hunting down what's going on with Qt 5.12.3, you can check that your code is working correctly using your distribution provided Qt.

    On a side note you can use:

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

    To see the version used to build Qt and the one currently loaded.


Log in to reply