1、SSL配置
QSslConfiguration conf = webclient.sslConfiguration();
conf.setPeerVerifyMode(QSslSocket::VerifyNone);
//conf.setPeerVerifyMode(QSslSocket::VerifyPeer);
conf.setProtocol(QSsl::TlsV1SslV3);
webclient.setSslConfiguration(conf);
如果setPeerVerifyMode设置为VerifyPeer,配置信赖服务器证书,我这里使用的是自签名证书,做如下配置
QList<QSslCertificate> certlist = QSslCertificate::fromPath(":/localhost.cert");
QSslError error(QSslError::SelfSignedCertificate, certlist.at(0));
QList<QSslError> expectedErrors;
expectedErrors.append(error);
webclient.ignoreSslErrors(expectedErrors);
2、完整构造函数实现
//配置SSL
QSslConfiguration conf = webclient.sslConfiguration();
//conf.setPeerVerifyMode(QSslSocket::VerifyNone);
conf.setPeerVerifyMode(QSslSocket::VerifyPeer);
conf.setProtocol(QSsl::TlsV1SslV3);
webclient.setSslConfiguration(conf);
//解决自签名证书不受信赖问题,VerifyNone不需添加以下代码
QList<QSslCertificate> certlist = QSslCertificate::fromPath(":/localhost.cert");
QSslError error(QSslError::SelfSignedCertificate, certlist.at(0));
QList<QSslError> expectedErrors;
expectedErrors.append(error);
webclient.ignoreSslErrors(expectedErrors);
connect(&webclient, SIGNAL(textMessageReceived(QString)), this, SLOT(recvData(QString)));
connect(&webclient, SIGNAL(connected()), this, SLOT(connected()));
connect(&webclient, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(readError(QAbstractSocket::SocketError)));
源代码下载:https://download.csdn.net/download/cqchengdan/11439289