HTTPS POST/GET 与 Qml/Qt [英] Https POST / GET with Qml/Qt

查看:18
本文介绍了HTTPS POST/GET 与 Qml/Qt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我正在使用 Qt-Qml 开发诺基亚手机.我必须向给定的 HTTPS Url 发出 POST 请求.我正在使用 QML,但我正在尝试用 Javascript 来完成它,但没有任何运气.

Recently I'm working on Nokia mobiles using Qt-Qml. I have to make a POST request to a given HTTPS Url. I'm using QML and I'm trying to do it in Javascript without any luck.

有人对此有什么想法吗?可以在 QML 中使用 Javascript 来实现吗?关于如何在 QT 中制作它的任何建议?

Anyone has an idea about it? It's possible to do it using Javascript in QML? Any advise on how to make it in QT?

我尝试调用这样的函数:

I tried calling a function like this:

var http = new XMLHttpRequest()
var url = "myform.xsl_submit";
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 && http.status == 200) {
        print("ok");
    }else{
                print("cannot connect");
        }
}
http.send(params);

推荐答案

你的if语句有误:该函数被调用了多次,但只调用了一次http.readyState = 4.因此,尽管还没有错误,但您仍会打印错误消息.

Your if statement is wrong: The function is called several times, but only one time http.readyState = 4. So, you print error messages although there's no errors yet.

你应该先检查是否http.readyState = 4,然后再看状态码.

You should first check if http.readyState = 4, and then look at the status code.

这是一个最小的工作示例:

Here is a minimal working example:

import QtQuick 1.1

Rectangle {
    Component.onCompleted: {
        var http = new XMLHttpRequest()
        var url = "http://localhost:8080";
        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);
    }
}

我用 netcat 创建了一个本地伪网络服务器来测试它:

I created a local pseudo-webserver with netcat to test it:

% echo -e 'HTTP/1.1 200 OK

' | nc -l 8080 
POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
Content-Length: 15
Connection: Keep-Alive
Accept-Encoding: gzip
Accept-Language: de-DE,en,*
User-Agent: Mozilla/5.0
Host: localhost:8080

num=22&num2=333

这篇关于HTTPS POST/GET 与 Qml/Qt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆