Qt 忽略 SSL 错误

spa*_*pan 3 ssl qt signals-slots

我已经尝试过在 Qt 中忽略 SSL 握手错误的几种不同尝试,但没有成功。我正在使用自签名证书,在开发过程中我只想忽略任何错误,以防我必须更改证书或发生其他情况。

我看过:

抑制 SSL 错误

http://qt-project.org/doc/qt-5/qnetworkaccessmanager.html#sslErrors

https://qt-project.org/forums/viewthread/17456

奇怪的 Qt SSL 问题 - 显示错误“No Error”,没有其他内容,如果我忽略它,一切正常

http://www.qtcentre.org/threads/48736-Ignore-SSL-Errors

http://developer.nokia.com/community/wiki/How_to_ignore_ssl_errors_to_get_https_website_work_on_QML_Webview

http://xizhizhu.blogspot.se/2010/08/basic-samples-for-ssl-communication.html

这是我的请求代码

mManager = new QNetworkAccessManager(this);
... // adding onRequestComplete slot and creation of postdata and request
QObject::connect(mManager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this, SLOT(onSslError(QNetworkReply*, QList<QSslError>)));

QNetworkReply *reply = mManager->post(request, postData);
//QObject::connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(onSslError(QList<QSslError>)));
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我尝试连接到网络管理器信号和回复信号。这是我的插槽:

void onSslError(QList<QSslError> errors) {
    QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
    reply->ignoreSslErrors();
}

void onSslError(QNetworkReply* r, QList<QSslError> l) {
    r->ignoreSslErrors();
}
Run Code Online (Sandbox Code Playgroud)

当我发出正常的 HTTP 请求时。将请求中的 QUrl 更改为 https 会导致我的问题。我在 onRequestComplete 槽中收到的错误是“无法启动 SSL 上下文”。

我尝试在两个插槽中设置断点,但它们从未触发,在我看来,ignoreSslErrors 调用永远不会执行。任何帮助是极大的赞赏。

IGH*_*HOR 6

最好这样使用它:

QList<QSslError> errorsThatCanBeIgnored;

errorsThatCanBeIgnored << QSslError(QSslError::HostNameMismatch, cert);
errorsThatCanBeIgnored << QSslError(QSslError::SelfSignedCertificate, cert);
    
ignoreSslErrors(errorsThatCanBeIgnored);
Run Code Online (Sandbox Code Playgroud)