Thread: SSL with QML
View Single Post
marxian's Avatar
Posts: 2,448 | Thanked: 9,523 times | Joined on Aug 2010 @ Wigan, UK
#3
Originally Posted by xiph View Post
Hi, sorry for waking up this old thread. But I was also wondering about this, so I thought that it would be better to write here than starting a new thread.

My app is running on a Jolla.
I'm not sure about whether it, in my case, has to do with that I'm using a self signed certificate or not. I can access the https url through the browser without problem since I've installed the certificate in the phone.
It could be that the QNetworkAccessManger (XMLHttpRequest uses standard QtNetwork APIs behind the scenes) is getting an SSL error. When this happens, it emits the sslErrors(QNetworkReply*,QList<QSslError>)) signal. In my experience, this is not handled in the QtDeclarative/QtQuick APIs for XMLHttpRequest, but you can handle it yourself by subclassing QDeclarativeNetworkAccessManagerFactory and using QDeclarativeEngine::setNetworkAccessManagerFactory (). Your subclass should handle the SSL errors (you can just ignore them if you like).

Example (not tested):

networkaccessmanagerfactory.h

Code:
#ifndef NETWORKACCESSMANAGERFACTORY_H
#define NETWORKACCESSMANAGERFACTORY_H

#include <QDeclarativeNetworkAccessManagerFactory>

class NetworkAccessManagerFactory : public QDeclarativeNetworkAccessManagerFactory
{

public:
    QNetworkAccessManager* create(QObject *parent);

private slots:
    void onSSLErrors(QNetworkReply *reply, const QList<QSslError> &errors);
};

#endif // NETWORKACCESSMANAGERFACTORY_H
networkaccessmanagerfactory.cpp

Code:
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include "networkaccessmanagerfactory.h"

QNetworkAccessManager* NetworkAccessManagerFactory::create(QObject *parent) {
    QNetworkAccessManager *manager = new QNetworkAccessManager(parent);
    connect(manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this, SLOT(onSSLErrors(QNetworkReply*,QList<QSslError>)));
    return manager;
}

void NetworkAccessManagerFactory::onSSLErrors(QNetworkReply *reply, const QList<QSslError> &errors) {
    reply->ignoreSslErrors(errors);
}
In your main() function:

Code:
NetworkAccessManagerFactory factory;
QDeclarativeView view;
QDeclarativeEngine *engine = view.engine();
engine->setNetworkAccessManagerFactory(&factory);
Now, all SSL errors will be ignored, and any requests initiated from QML/JS should be successful.
__________________
'Men of high position are allowed, by a special act of grace, to accomodate their reasoning to the answer they need. Logic is only required in those of lesser rank.' - J K Galbraith

My website

GitHub