Results 1 to 4 of 4

Thread: Using XMLHttpRequest for HTTPS Post to server with SSL certificate

  1. #1
    Join Date
    Sep 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Using XMLHttpRequest for HTTPS Post to server with SSL certificate

    Hi all,

    I'm developing an app for Symbian S60 5th and Sym^3 phones using Nokia Qt SDK v.1.1.3.

    My app needs to communicate with a server through HTTPS Post. The server has a certificate (.cer file) to be used for verification and encryption.

    I'm trying to implement the network communication in QML/JS using XmlHttpRequest but it does not work: it is interrupted during handshake with readystate = DONE but with status = 0 and status text = 0.

    My code is:
    Qt Code:
    1. var https = new XMLHttpRequest();
    2. https.open("POST", url, true);
    3.  
    4. https.setRequestHeader("Content-type", "application/json");
    5. https.onreadystatechange = function() {
    6. if (https.readyState == XMLHttpRequest.HEADERS_RECEIVED) {
    7. var status = https.status;
    8. if(status != 200) {
    9. app.logWrite("HttpsPostToService; Headers received; Error. Status: " + status + ". Status text: " + https.statusText, 4);
    10. return;
    11. }
    12. } else if (https.readyState == XMLHttpRequest.DONE) {
    13. var status = https.status;
    14. if(status != 200) {
    15. app.logWrite("HttpsPostToService; Done; Not OK. Status: " + status + ". Status text: " + https.statusText, 4);
    16. return;
    17. }
    18. var data = null;
    19. data = https.responseText;
    20. app.logWrite("HttpsPostToService; Done; OK. Received following data:", 4);
    21. app.logWrite("HttpsPostToService; " + data, 4);
    22. }
    23. }
    24. https.send(jsonpar);
    To copy to clipboard, switch view to plain text mode 

    I tried successfully to load server certificate among the default CA Certificates for QSSLSockets but this does not solve the problem (see code below).
    I was thinking QML to use QSSLSocket in SSL communication but perhaps I'm wrong.

    My question is: is it possible to manage SSL with XMLHttpRequest? If yes how can I add the certificate in order to use it?
    Or have I to go with a Qt implementation?

    Thanks in advance for any support.


    Qt Code:
    1. if (QFile::exists("C:/Data/Certs/certificate.cer"))
    2. {
    3. QFile certFile("C:/Data/Certs/certificate.cer");
    4. if (certFile.open(QIODevice::ReadOnly))
    5. {
    6. QSslCertificate cert(&certFile, QSsl::Der);
    7. if (! cert.isNull())
    8. {
    9. qDebug() << "Cert not null";
    10. if (cert.isValid())
    11. {
    12. qDebug() << "Cert is valid";
    13. QSslSocket::addDefaultCaCertificate(cert);
    14. qDebug() << "Cert was added. Info: " << cert.serialNumber();
    15. }
    16. else
    17. {
    18. qDebug() << "Cert is NOT valid";
    19. }
    20.  
    21.  
    22. }
    23. else
    24. {
    25. qDebug() << "Cert is null";
    26. }
    27. if (certFile.isOpen())
    28. certFile.close();
    29. }
    30. }
    31.  
    32.  
    33. qDebug() << "Root CA certificates: \n" << QSslSocket::defaultCaCertificates();
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Using XMLHttpRequest for HTTPS Post to server with SSL certificate

    Are you sure this is the issue with certificates? What happens if you don't use SSL at all?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Symbian S60

    Default Re: Using XMLHttpRequest for HTTPS Post to server with SSL certificate

    Quote Originally Posted by wysota View Post
    Are you sure this is the issue with certificates? What happens if you don't use SSL at all?
    Hi wysota,

    thanks for your response.

    If I do a simple GET to http://www.google.com all is OK.

    If I do a HTTPS GET to for example https://market.android.com/?hl=it also it works (Done OK)

    My target service is reachable through POST both via HTTP and via HTTPS at the same link. I must use HTTPS for security reasons.

    If I do a HTTP POST to it all is OK.

    If I do a HTTPS POST it doesn't work as I was explaining above.

    I can do the same HTTPS POST through REST Client Firefox Addin (but the browser asks me to confirm secury exception for unverified server certificate).

    At this point I suspect issue with certificate.

  4. #4
    Join Date
    Nov 2014
    Posts
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Android

    Exclamation Re: Using XMLHttpRequest for HTTPS Post to server with SSL certificate

    how did you make the POST with XMLHttpRequest for HTTP post i've created a app for androit with QT+QML and is not Working at all :

    function sendDataPost()
    {
    var doc = new XMLHttpRequest();
    doc.onreadystatechange = function() {
    if (doc.readyState == XMLHttpRequest.HEADERS_RECEIVED) {
    console.log("Headers 1 -->");
    console.log(doc.getAllResponseHeaders ());
    console.log("Last modified -->");
    console.log(doc.getResponseHeader ("Last-Modified"));
    console.log('responseText');
    console.log(doc.responseText);
    } else if (doc.readyState == XMLHttpRequest.DONE) {
    var a = doc.responseXML.documentElement;
    for (var ii = 0; ii < a.childNodes.length; ++ii) {
    console.log(a.childNodes[ii].nodeName);
    }
    console.log("Headers 2 -->");
    console.log(doc.getAllResponseHeaders ());
    console.log("Last modified -->");
    console.log(doc.getResponseHeader ("Last-Modified"));
    }
    }

    doc.open("POST", "http://104.131.179.18/velneo/proceso?dos=hola");
    doc.send("dos=Comoestas");


    Do you see if im doing something wrong or i need to do something different? im really stock in this or! the problem is in my server?

Similar Threads

  1. QWebView + HTTPS with certificate example
    By kylls in forum Qt Programming
    Replies: 1
    Last Post: 16th March 2011, 07:07
  2. Replies: 0
    Last Post: 28th February 2011, 10:10
  3. Https server - logging by QSsl
    By Trok in forum Qt Programming
    Replies: 2
    Last Post: 24th September 2010, 00:22
  4. Replies: 0
    Last Post: 2nd February 2010, 23:40
  5. Https POST Request
    By munna in forum Qt Programming
    Replies: 10
    Last Post: 11th November 2006, 15:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.