Qt如何使用SSL发送简单的HTTPS后发布?
该程序应该使用QNetworkAccessManager通过HTTPS发送帖子。
以下是我的代码的一个简单示例。
QUrlQuery params;
QByteArray dane;
params.addQueryItem("mWyslijpost", ui->lineEdit->text());
dane.append(params.toString());
QUrl url("https://81.2.244.83/Testpostssl/infotest.php");
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
QNetworkReply *reply = manager->post(request, dane);
QFile certFile(":/crt/cert.crt");
Q_ASSERT(certFile.open(QIODevice::ReadOnly));
QSslCertificate cert(&certFile, QSsl::Pem);
QSslSocket * sslSocket = new QSslSocket(this);
sslSocket->addCaCertificate(cert);
sslSocket->connectToHostEncrypted(SERVER, 443);
if (!sslSocket->waitForEncrypted())
{
qDebug() << "Info " << sslSocket->errorString();
return false;
}
QSslConfiguration config = sslSocket->sslConfiguration();
config.setProtocol(QSsl::TlsV1_2);
sslSocket->setSslConfiguration(config);
reply->setSslConfiguration(config);
// wait
QEventLoop elCzekaj;
connect(reply, SIGNAL(finished()), &elCzekaj, SLOT(quit()));
elCzekaj.exec();
if(reply->error() == QNetworkReply::NoError)
{
qDebug() << reply->readAll();
}
else
{
qDebug() << "Error...";
qDebug() << reply->errorString();
}
如何从服务器下载证书? 如何配置QSsl?
答案 0 :(得分:0)
要从同行那里获得证书,请尝试以下方法:
QSslSocket *socket = new QSslSocket();
QObject::connect(socket,
&QSslSocket::encrypted,
[=](){qDebug() << socket->peerCertificate() << " cert";}
);
socket->connectToHostEncrypted("stackoverflow.com", 443);
关于问题:“如何配置QSsl”,这取决于您想要做什么。如果您只是想通过TLS从Web服务器获取内容,只需使用QNetworkAccessManager即可。那里有默认值。