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

问题描述

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



任何人都有一个想法吗?可以在QML中使用Javascript来完成它?
任何建议如何使它在QT?



我试着调用这样的函数:

  c $ c>语句错误:该函数被多次调用,但只有一次 http.readyState = 4 。因此,尽管没有错误,但您仍然可以打印错误消息。



您应该首先检查 http.readyState = 4 ,然后查看状态代码。



这是一个最基本的工作示例:

  import QtQuick 1.1 

Rectangle {
Component.onCompleted:{
var http = new XMLHttpRequest()
var url =http://本地主机:8080\" ;
var params =num = 22& num2 = 333;
http.open(POST,url,true);

//发送正确的头信息以及请求
http.setRequestHeader(Content-type,application / x-www-form-urlencoded);
http.setRequestHeader(Content-length,params.length);
http.setRequestHeader(Connection,close);

http.onreadystatechange = function(){//当状态改变时调用一个函数。
if(http.readyState == 4){
if(http.status == 200){
console.log(ok)
} else {
console.log(error:+ http.status)
}
}
}
http.send(params);




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


 %echo -e'HTTP / 1.1 200 OK\\\
\\\
'| nc -l 8080
POST / HTTP / 1.1
内容类型:application / x-www-form-urlencoded; charset = UTF-8
内容长度:15
连接:Keep-Alive
Accept-Encoding:gzip
Accept-Language:de-DE,en,*
User-Agent:Mozilla / 5.0
Host:localhost:8080

num = 22& num2 = 333


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.

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);
解决方案

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.

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);
    }
}

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

% echo -e 'HTTP/1.1 200 OK\n\n' | 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 05:50