Qt Quick 网络请求
方式一:在QML中进行
Button{
text:"发送请求"
onClicked: {
let xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
//XMLHttpRequest.DONE为状态枚举值4
if(xhr.readyState === 4) {
print('request DONE',xhr.status);
if(xhr.status === 200){
txt.text = xhr.responseText.toString();
}
}
}
//【get】
xhr.open("GET", "http://127.0.0.1:3333/get?q=m");
xhr.send();
//【post】
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.open("POST", "http://127.0.0.1:3333/post");
xhr.send("q=m");
}
}
qml
上面的方法存在局限性,对本地openssl版本有要求
发送请求失败时,可以考虑以下方法尝试解决:
首先在main函数里面输出当前需要的openssl版本:
qDebug()<<"QSslSocket="<<QSslSocket::sslLibraryBuildVersionString();
c++