• Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Register
  • Login
UBports Robot Logo UBports Forum
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Register
  • Login

HTTPS requests with XMLHttpRequest [Resolved]

Scheduled Pinned Locked Moved App Development
17 Posts 8 Posters 10.8k Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • I Offline
      Ingo
      last edited by 10 May 2019, 07:11

      Maybe this code helps debugging the problem with HTTPS?
      https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/How_to_check_the_secruity_state_of_an_XMLHTTPRequest_over_SSL

      readyState=4 while status is still 0 sounds a bit as if the request never was actually made to the server, e.g. something in the handshake went wrong.
      Maybe this points in the right direction: https://stackoverflow.com/questions/36416389/why-does-my-xmlhttprequest-have-readystate-4-but-status-0

      1 Reply Last reply Reply Quote 1
      • A Offline
        AppLee
        last edited by 10 May 2019, 16:22

        @dobey I don't know how a specific url will change anything, because it does not work with any https...
        The first test I did was for this app :
        https://gitlab.com/AppsLee/domoticz-ubports

        In this case, the certificate is not ok, so I didn't worry much.
        Now I'm creating a client for Slack, so I tried with a genuine HTTPS request (I use the same Request.qml as for domoticz, but now I put some debug code)
        The url tested is : https://slack.com/api/conversations.history?token=<myToken>&channel=<myChannelId>
        I also tested other HTTPS url : https://www.appliancetherapy.com and https://addons.mozilla.org for example, but none worked, but all of them worked with Morph browser.

        In each case I got the same answer: status=0 and readyState=DONE
        There no other kind of error message, I don't know what you expect.

        To trigger the request I use this:

        Component.onCompleted: reqFct.getMessages("https://addons.mozilla.org")
        

        The request is sent by:

        function getMessages(url) {
          var req = new XMLHttpRequest();
          req.open("get", url, true);
          req.onreadystatechange = function() {requestItem.readMessages(req)}
          req.send();
        } // getMessages()
        

        And the readMessages has been reduced to that:

        function readMessages(objReq, refresh) {
          if ( objReq.readyState === XMLHttpRequest.DONE ) {
            if (objReq.status == 200) {
              inputMsg.text += "Answer OK\n"
            } else {
              inputMsg.text += "Answer KO "+objReq.status+"\n"
            }
            inputMsg.text += "YY"+objReq.responseText+"XX\n"
            }
          } else {
            inputMsg.text += "NOT DONE yet\n"
          }
        } // readMessages()
        

        In inputMsg, I've got:

        Answer KO 0
        YYXX
        

        I removed the Content-type just to be sure (I added it hoping it might help) but same result.
        I tried various example from the net but nothing solved my issue, that's why I thought maybe I need to enable something or send some magic command to enable HTTPS.

        @Ingo thanks for the links, I'll read, try and go back here later this weekend.

        D 1 Reply Last reply 10 May 2019, 17:02 Reply Quote 0
        • I Offline
          Ingo
          last edited by 10 May 2019, 16:27

          @AppLee said in HTTPS requests with XMLHttpRequest:

          There no other kind of error message, I don't know what you expect.

          For that I guess you need to add an xhr.onerror callback (or whatever is needed to catch errors of XMLHttpRequest in JavaScript).

          1 Reply Last reply Reply Quote 0
          • D Offline
            dobey @AppLee
            last edited by 10 May 2019, 17:02

            @AppLee said in HTTPS requests with XMLHttpRequest:

            In each case I got the same answer: status=0 and readyState=DONE
            There no other kind of error message, I don't know what you expect.

            This generally means that what happened was a connection to the server was not possible. So you of course will not get the response status code from the server, and instead you need to handle the onerror callback for the XMLHttpRequest object, to get an error. Most common cause there is connecting to a site with cert that couldn't be verified.

            1 Reply Last reply Reply Quote 0
            • A Offline
              AppLee
              last edited by 10 May 2019, 17:08

              @dobey and @Ingo

              Thank you both, I'm looking into the code @dobey posted to implement the onerror so I can go forward.
              I'll give you an update tomorrow after spending some time on this.

              1 Reply Last reply Reply Quote 0
              • A Offline
                AppLee
                last edited by 16 May 2019, 18:16

                Hi, I promised a feedback.

                My first tests weren't successful, I need to work on how to implement the onerror method to have better understanding of my issue.

                Thanks again for your ideas

                1 Reply Last reply Reply Quote 2
                • A Offline
                  AppLee
                  last edited by 10 Jun 2019, 15:34

                  Ok guys, I finally found my mistake, so here is the story.
                  After too much time spent on this issue, I decided to use the "big gun" to move forward.

                  So, this weekend I started to create a C++ plugin based on QNetworkAccessManager.
                  And I did the same error, so I got nothing in return.
                  Few coding later (after implementing the error management) I had my answer : Host not found !?!

                  I tried with another HTTPS URI and I got an access denied error...

                  After a few try I finally understood how stupid I had been.
                  I forgot to add "networking" in the app apparmor !!!

                  In fact my many tries and switching from a working app to a WIP app hid the real problem.

                  But it seems there is a little tricky thing:
                  When you change apparmor or the app logo, you have to uninstall the app before pushing it again to debug, changes have no effect otherwise...

                  Many thanks for all the help guys. I'll be more cautious next time

                  D 1 Reply Last reply 10 Jun 2019, 17:16 Reply Quote 2
                  • D Offline
                    dobey @AppLee
                    last edited by 10 Jun 2019, 17:16

                    @AppLee said in HTTPS requests with XMLHttpRequest [Resolved]:

                    When you change apparmor or the app logo, you have to uninstall the app before pushing it again to debug, changes have no effect otherwise...

                    There is a known issue, where you need to change the version number whenever you change the apparmor, to cause the profile to be regenerated.

                    1 Reply Last reply Reply Quote 2
                    • W Offline
                      wangqinfeng
                      last edited by 24 Jun 2019, 13:25

                      谢谢您!解决了我的问题!☺

                      A 1 Reply Last reply 24 Jun 2019, 14:19 Reply Quote 0
                      • A Offline
                        advocatux @wangqinfeng
                        last edited by 24 Jun 2019, 14:19

                        @wangqinfeng hi, welcome and remember that this Forum is English only 😉

                        It seems you're saying something like Thank you! Solved my problem! and that's why I didn't remove your post but, please, keep in mind that English requirement 🙂

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post