我有一个带有ssl/https并由htaccess保护的家庭服务器。我已经以一种方式配置了服务器,如果我访问
https://USER:PASSWORD@subdomain.domain.con/room_1/switch_1/set.php?state=0
房间 1 的灯熄灭了。此外,页面返回的当前状态非常简单,例如
<html>
0
</html>
现在我正在尝试使用 QML 构建一个客户端应用程序(因为它将支持多个平台窗体)。
任务应该是按下一个按钮,应用程序"拨入"到
https://USER:PASSWORD@subdomain.domain.con/room_1/switch_1/set.php?state=0
并在之后将 PHP 页面读出为纯字符串。
我从这里尝试了XMLHttpRequest,例如
var req = new XMLHttpRequest();
req.open("POST", "http://www.xxxxxxxxx.com/test/test.php");
req.onreadystatechange = function() {
if (req.readyState == XMLHttpRequest.DONE) {
// what you want to be done when request is successfull
}
}
req.onerror = function(){
// what you want to be done when request failed
}
req.send("name=xxx&email=xxx&message=xxx");
在按钮单击事件中,但没有响应。
问题哪种方式可以实现此任务???
求解忽略自动签名的 SSL 证书以及 XMLHttpRequest 的一些基础知识。
要处理自签名(或任何)SSL CA,请执行以下操作:
将这些包含添加到主.cpp
#include <QSslConfiguration>
#include <QSslSocket>
将这些行添加到 main() 的开头
QSslConfiguration config = QSslConfiguration::defaultConfiguration();
config.setPeerVerifyMode(QSslSocket::VerifyNone);
QSslConfiguration::setDefaultConfiguration(config);
要通过 htaccess 授权读取文件/响应,请执行以下操作:
该行"xhr.setRequestHeader( 'Authorization', 'Basic ' + Qt.btoa(tb_user.text + ':' + tb_pw.text) )"是正确设置用户和密码的关键。
function getSome() {
txtLog.log("getSome executed");
var xhr = new XMLHttpRequest()
xhr.open( 'POST', 'https://subdomain.domain.con/room_1/switch_1/set.php?state=0', true)
xhr.setRequestHeader( 'Authorization', 'Basic ' + Qt.btoa(tb_user.text + ':' + tb_pw.text) )
xhr.withCredentials = true
xhr.onreadystatechange = function() {
console.log( xhr.status, xhr.statusText )
console.log( xhr.responseText , xhr.responseText )
}
xhr.send()
}
所有 Java 用户的旁注:
昨天我创建了一个Android Java应用程序,它可以很好地处理任务。自签名 SSL CA 的问题在私有 void trustEveryone() 函数中处理。
package com.home.me.myhome2;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Base64;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.X509TrustManager;
public class HomeActivity extends Activity {
private Button btnOn;
private Button btnOff;
private TextView tv_test;
private void trustEveryone() {
try {
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){
public boolean verify(String hostname, SSLSession session) {
return true;
}});
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, new X509TrustManager[]{new X509TrustManager(){
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}}}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(
context.getSocketFactory());
} catch (Exception e) { // should never happen
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
trustEveryone();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
btnOn = (Button) findViewById(R.id.btnOn);
btnOff = (Button) findViewById(R.id.btnOff);
tv_test = (TextView) findViewById(R.id.tv_test);
btnOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new PostClass().execute(new String[]{"https://subdomain.domain.con/room_1/switch_1/set.php?state=1"});
}
});
btnOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new PostClass().execute(new String[]{"https://subdomain.domain.con/room_1/switch_1/set.php?state=0"});
}
});
}
private class PostClass extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
try {
final TextView outputView = (TextView) findViewById(R.id.postOutput);
URL url = new URL(params[0]);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
String urlParameters = "";
connection.setRequestMethod("POST");
connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5");
final String basicAuth = "Basic " + Base64.encodeToString("USER:PASSWORD".getBytes(), Base64.URL_SAFE);
connection.setRequestProperty("Authorization", basicAuth);
connection.setDoOutput(true);
DataOutputStream dStream = new DataOutputStream(connection.getOutputStream());
dStream.writeBytes(urlParameters);
dStream.flush();
dStream.close();
int responseCode = connection.getResponseCode();
final StringBuilder output = new StringBuilder("Request URL " + url);
output.append(System.getProperty("line.separator") + "Request Parameters " + urlParameters);
output.append(System.getProperty("line.separator") + "Response Code " + responseCode);
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = "";
final StringBuilder responseOutput = new StringBuilder();
while ((line = br.readLine()) != null) {
responseOutput.append(line);
}
br.close();
output.append(System.getProperty("line.separator") + "Response " + System.getProperty("line.separator") + System.getProperty("line.separator") + responseOutput.toString());
HomeActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
outputView.setText(output);
;
}
});
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
}
}