Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]Qt 4.8 https post question
Qt 6.11 is out! See what's new in the release blog

[Solved]Qt 4.8 https post question

Scheduled Pinned Locked Moved General and Desktop
2 Posts 1 Posters 3.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cidhuang
    wrote on last edited by
    #1

    Hi,

    I referenced a "discussion":http://www.qtforum.org/article/34097/ssl-server.html 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.

    Thanks,

    Cid

    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("https://192.168.1.108:8080/test");
    
    // 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, cert.at (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;
    sslConfig.setPrivateKey(key&#41;;
    sslConfig.setLocalCertificate(cert2);
    QList<QSslCertificate> ca;
    ca.append(cert);
    ca.append(cert2);
    sslConfig.setCaCertificates(ca);
    request.setSslConfiguration(sslConfig);
    
    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();

    app.use(express.bodyParser());

    // Authenticator
    app.use(express.basicAuth('john', 'doe'));

    app.post('/test', function(req, res) {
    var body = '{
    "result":"ok"
    }';

    res.send(body);
    

    });

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

    1 Reply Last reply
    0
    • C Offline
      C Offline
      cidhuang
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved