[Solved]Qt 4.8 https post question
-
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(":/basic/client.key"); QSslKey key(&keyFile, QSsl::Rsa, QSsl::Pem, QSsl::PrivateKey, "passPhrase"); QFile certFile(":/basic/client_sign.crt"); QSslCertificate cert2(&certFile); QSslConfiguration sslConfig; sslConfig.setPrivateKey(key); 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);
@