gpt4 book ai didi

qt - 使用 Qml/Qt 进行 HTTPS POST/GET

转载 作者:行者123 更新时间:2023-12-02 03:00:35 25 4
gpt4 key购买 nike

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

有人对此有想法吗?可以在 QML 中使用 Javascript 来做到这一点吗?关于如何在 QT 中制作它有什么建议吗?

我尝试调用这样的函数:

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。因此,尽管还没有错误,但您还是打印了错误消息。

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

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

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 创建了一个本地伪网络服务器来测试它:

% 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

关于qt - 使用 Qml/Qt 进行 HTTPS POST/GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8790609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com