maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Development (https://talk.maemo.org/forumdisplay.php?f=13)
-   -   SSL with QML (https://talk.maemo.org/showthread.php?t=70074)

Slocan 2011-02-19 04:18

SSL with QML
 
Has anyone been successful in fetching an SSL page using XmlHttpRequest in QML on N900? I've got the following bit of code, which works fine for non-SSL page, but the response for SSL pages is empty (status code of 0, and empty headers). It works fine on the desktop and the N8. Am I missing something on the N900, or a bug on that platform?

Code:

import Qt 4.7

Rectangle {
    width: 360
    height: 360
    id: screen
    property string addText: ""

    Text {
        id: textDisplay
        anchors.centerIn: parent
        text: "Status code: " + parent.addText
    }

    Component.onCompleted: {
                var url = 'https://maemo.org/'
                var xhr = new XMLHttpRequest;
                xhr.open("GET", url);
                xhr.onreadystatechange = function() {
                    if (xhr.readyState == XMLHttpRequest.DONE) {
                        screen.addText = xhr.status
                    }
                }
                xhr.send();
    }
}


xiph 2014-06-18 14:05

Re: SSL with QML
 
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.

marxian 2014-06-18 15:27

Re: SSL with QML
 
Quote:

Originally Posted by xiph (Post 1430013)
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. :)

xiph 2014-06-18 16:13

Re: SSL with QML
 
I managed to ignore the SSL errors, so it works for now. But this seems to be a bit dangerous. Aren't there a way to look up whether the certificate is installed and accepted in the phone?


All times are GMT. The time now is 06:34.

vBulletin® Version 3.8.8