我看过几种不同的忽略 Qt 中的 SSL 握手错误的尝试,但都没有奏效。我使用的是自签名证书,在开发过程中我只想忽略任何错误,以防我必须更改证书或发生其他事情。
我看过:
http://qt-project.org/doc/qt-5/qnetworkaccessmanager.html#sslErrors
https://qt-project.org/forums/viewthread/17456
Weird Qt SSL issue -- error "No Error" shows up, nothing else, and if I ignore it, everything works
http://www.qtcentre.org/threads/48736-Ignore-SSL-Errors
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>)));
如您所见,我已尝试连接到 networkmanager 信号和回复信号。这是我的广告位:
void onSslError(QList<QSslError> errors) {
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
reply->ignoreSslErrors();
}
void onSslError(QNetworkReply* r, QList<QSslError> l) {
r->ignoreSslErrors();
}
当我发出正常运行的常规 HTTP 请求时。将请求中的 QUrl 更改为 https 会导致我的问题。我在 onRequestComplete 插槽中遇到的错误是“Could not initiate SSL context”。
我已经尝试在两个插槽中设置断点,但它们永远不会触发,在我看来,ignoreSslErrors 调用永远不会执行。非常感谢任何帮助。
最佳答案
最好这样使用:
QList<QSslError> errorsThatCanBeIgnored;
errorsThatCanBeIgnored << QSslError(QSslError::HostNameMismatch, cert);
errorsThatCanBeIgnored << QSslError(QSslError::SelfSignedCertificate, cert);
ignoreSslErrors(errorsThatCanBeIgnored);
关于Qt 忽略 SSL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24122861/