سؤال

I am trying to write a simple mobile application to control some basic VLC functions. I'm really new at this and just trying to wrap my head around the whole thing but i've been reading around and found some code:

This is the qml code and I made basic changes at parts which I understood:

Component.onCompleted: {
    var http = new XMLHttpRequest()
    var url = "http://" + ip + ":" + port;
    var params = "num=22&num2=333";
    http.open("POST", url, true);

    // Send the proper header information along with the request
    http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");

    http.onreadystatechange = function() { // Call a function when the state changes.
        if (http.readyState == 4) {
            if (http.status == 200) {
                console.log("ok")
            } else {
                console.log("error: " + http.status)
            }
        }
    }
    http.send(params);
}

Also found some java code:

webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
                string credentials = String.Format("{0}:{1}", username, password);
                byte[] bytes = Encoding.UTF8.GetBytes(credentials);
                string base64 = Convert.ToBase64String(bytes);
                string authorization = String.Concat("Basic ", base64);
                webClient.Headers["Authorization"] = authorization;
                string url = "http://" + ip + ":" + port + "/requests/status.xml";
                var uri = new Uri(url);
                webClient.DownloadStringAsync(uri);

The part I am currently lost with QML is I got no idea how to pass the credentials to login as VLC requires a password.

Would anyone be able to help me out how to change the qml code to connect to the http client and then be able to pass the commands like:

http://localhost:8080/requests/status.xml?command=pl_play

Thanks :)

هل كانت مفيدة؟

المحلول

Not that proficient with QML, so I can only provide pseudo-code. The simplest form of HTTP authentication is through the Authorization header containing the string Basic followed by a whitespace and username and password separated by a colon : in base64.

Assuming base64() returns the b64 representation of passed data, this should do:

 http.setRequestHeader("Authorization", "Basic " + base64(username + ":" + password));
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top