Reply
Thread Tools
Posts: 324 | Thanked: 371 times | Joined on Dec 2009 @ Vancouver, BC
#1
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();
    }
}
 
Posts: 19 | Thanked: 7 times | Joined on Oct 2009 @ Sweden
#2
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'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
 
Posts: 19 | Thanked: 7 times | Joined on Oct 2009 @ Sweden
#4
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?
 
Reply


 
Forum Jump


All times are GMT. The time now is 22:11.