我直接从我的qml视图中发出一些https请求,例如图像源。由于我有自签名证书服务器端,我需要告诉qt忽略一些ssl错误(我控制服务器和客户端应用程序,所以这不应该是一个问题)。
我已经制作了一个QQmlNetworkAccessManagerFactory来创建NAM,我连接到sslErrors信号。
UltraQmlAccessManagerFactory.h:
#ifndef FACKFACKTORy_H
#define FACKFACKTORy_H
#include <QQmlNetworkAccessManagerFactory>
#include <QObject>
#include <QNetworkReply>
#include <QList>
#include <QSslError>
#include <QNetworkAccessManager>
#include <QDebug>
#include <QSslCertificate>
class UltraQmlNetworkAccessManagerFactory : public QObject,
public QQmlNetworkAccessManagerFactory {
Q_OBJECT
private:
QNetworkAccessManager* nam;
QList<QSslError> expectedSslErrors;
public:
explicit UltraQmlNetworkAccessManagerFactory();
~UltraQmlNetworkAccessManagerFactory();
virtual QNetworkAccessManager* create(QObject* parent);
public slots:
void onIgnoreSslErrors(QNetworkReply* reply, QList<QSslError> errors);
};
#endif
UltraQmlNetworkAccessManagerFactory.cpp:
#include "UltraQmlNetworkAccessManagerFactory.h"
UltraQmlNetworkAccessManagerFactory::UltraQmlNetworkAccessManagerFactory() {
}
UltraQmlNetworkAccessManagerFactory::~UltraQmlNetworkAccessManagerFactory() {
delete nam;
}
QNetworkAccessManager* UltraQmlNetworkAccessManagerFactory::create(QObject* parent) {
QNetworkAccessManager* nam = new QNetworkAccessManager(parent);
QObject::connect(nam, SIGNAL(sslErrors(QNetworkReply*, QList<QSslError>)),
this, SLOT(onIgnoreSslErrors(QNetworkReply*,QList<QSslError>))
);
return nam;
}
void UltraQmlNetworkAccessManagerFactory::onIgnoreSslErrors(QNetworkReply *reply, QList<QSslError> errors) {
for (int i = 0; i < errors.size(); i++) {
qDebug() << "e: " << errors.at(i) << endl;
}
reply->ignoreSslErrors(errors);
}
main.cpp中还有一些胶水可以设置这个工厂使用,我怀疑这部分是错误的来源,因为qDebug打印在输出中是可见的。
可以在函数/ slot onIgnoreSslErrors中的.cpp文件中看到,我尝试忽略我收到的每个错误(作为测试),但在输出中我没有得到预期的结果。
输出
e: "The certificate is self-signed, and untrusted"
qrc:/qml/file/ImageView.qml:16:5: QML Image: SSL handshake failed
我已成功使用QNetworkRequests
从C ++成员QSslConfiguration
,指定TLSV1_0和证书。由于我怀疑握手失败是因为一方需要SSL而其他TLS我也试图在QSslConfiguration
对象上设置QNetworkRequest
th reply->request();
然而,这没有改变