Important: Please read the Qt Code of Conduct -

[Solved]Qt 4.8 https post question

  • Hi,

    I referenced a "discussion": to make ssl key and cert. Then I built a node.js test server for testing my Qt 4.8 AP's https post function. But it always failed with ssl error message:

    "The host name did not match any of the valid hosts for this certificate"

    I tried Qt http post with node.js test server, it works.

    Please help me establish correct Qt 4.8 https post connection procedure.



    The Qt function code:
    QVariantMap data;
    data.insert("account", "test");
    data.insert("password", "testtest");

    QByteArray jsonString = QxtJSON::stringify(data).toUtf8();
    // For your "Content-Length" header
    QByteArray postDataSize = QByteArray::number(jsonString.size());
    QUrl serviceURL = QUrl("");
    // Time for building your request
    QNetworkRequest request(serviceURL);
    // Add the headers specifying their names and their values with the following method : void QNetworkRequest::setRawHeader(const QByteArray & headerName, const QByteArray & headerValue);
    request.setRawHeader("Content-Type", "application/json");
    request.setRawHeader("Content-Length", postDataSize);
    request.setRawHeader("Authorization", "Basic " + QByteArray(QString("%1:%2").arg("john").arg("doe").toAscii()).toBase64());
    QList<QSslCertificate> cert = QSslCertificate::fromPath (QLatin1String (":/basic/ca.crt"));
    QSslError error (QSslError::SelfSignedCertificate, (0));
    QList<QSslError> expectedSslErrors;
    expectedSslErrors.append (error);
    QFile keyFile&#40;":/basic/client.key"&#41;;
    QSslKey key(&keyFile, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey, "passPhrase"&#41;;
    QFile certFile(":/basic/client_sign.crt"&#41;;
    QSslCertificate cert2(&certFile&#41;;
    QSslConfiguration sslConfig;
    QList<QSslCertificate> ca;
    reply = nam->post(request, jsonString);
    reply->ignoreSslErrors (expectedSslErrors);


    The node.js test server code:
    var express = require('express');
    var https = require('https');
    var fs = require('fs');

    // This line is from the Node.js HTTPS documentation.
    var options = {
    key: fs.readFileSync('ssl/ca.key'),
    cert: fs.readFileSync('ssl/ca.crt'),
    passphrase: "gamdias",
    ca: fs.readFileSync('ssl/ca.crt')

    // Create a service (the app object is just a callback).
    var app = express();


    // Authenticator
    app.use(express.basicAuth('john', 'doe'));'/test', function(req, res) {
    var body = '{



    https.createServer(options, app).listen(8080);

  • The code is ok.
    The problem is key generation procedures.

Log in to reply