Javascript htaccess apache2服务器后面的QML/js通过https请求访问和读取html/php页面
我有一个带有ssl/https的家庭服务器,由htaccess保护。 我配置服务器的方式是,如果我访问Javascript htaccess apache2服务器后面的QML/js通过https请求访问和读取html/php页面,javascript,php,html,.htaccess,qml,Javascript,Php,Html,.htaccess,Qml,我有一个带有ssl/https的家庭服务器,由htaccess保护。 我配置服务器的方式是,如果我访问 https://USER:PASSWORD@subdomain.domain.con/room_1/switch_1/set.php?state=0 1号房间的灯熄灭了。 此外,页面返回当前状态非常简单,如 <html> 0 </html> 然后以普通字符串的形式读取php页面 我从XMLHttpRequest中进行了如下尝试 var req = new
https://USER:PASSWORD@subdomain.domain.con/room_1/switch_1/set.php?state=0
1号房间的灯熄灭了。
此外,页面返回当前状态非常简单,如
<html>
0
</html>
然后以普通字符串的形式读取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");
在按钮单击事件中,但没有响应
问题 哪种方法可以实现此任务???用 以及XMLHttpRequest的一些基础知识 要处理自签名(或任何)SSL CA: 将这些包含项添加到main.cpp
#include <QSslConfiguration>
#include <QSslSocket>
通过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的问题在private 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;
}
}
package com.home.me.myhome2;
导入android.app.Activity;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Base64;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
导入java.io.BufferedReader;
导入java.io.DataOutputStream;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.security.SecureRandom;
导入java.security.cert.CertificateException;
导入java.security.cert.x509证书;
导入javax.net.ssl.HostnameVerifier;
导入javax.net.ssl.HttpsURLConnection;
导入javax.net.ssl.SSLContext;
导入javax.net.ssl.SSLSession;
导入javax.net.ssl.X509TrustManager;
公共类HomeActivity扩展了活动{
私人按钮;
私人按钮btnOff;
私人文本视图电视测试;
个人无效信任所有人(){
试一试{
HttpsURLConnection.setDefaultHostnameVerifier(新的HostnameVerifier(){
公共布尔验证(字符串主机名、SSLSession会话){
返回true;
}});
SSLContext context=SSLContext.getInstance(“TLS”);
init(null,new X509TrustManager[]{new X509TrustManager(){
public void checkClientTrusted(X509Certificate[]链,
字符串authType)抛出CertificateException{}
公共无效checkServerTrusted(X509Certificate[]链,
字符串authType)抛出CertificateException{}
公共X509证书[]getAcceptedIssuers(){
返回新的X509证书[0];
}}},new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(
getSocketFactory());
}捕获(异常e){//永远不会发生
e、 printStackTrace();
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
信任所有人();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
btnOn=(按钮)findViewById(R.id.btnOn);
btnOff=(按钮)findViewById(R.id.btnOff);
tv_测试=(TextView)findviewbyd(R.id.tv_测试);
btnOn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
新建PostClass().execute(新字符串[]{”https://subdomain.domain.con/room_1/switch_1/set.php?state=1"});
}
});
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
新建PostClass().execute(新字符串[]{”https://subdomain.domain.con/room_1/switch_1/set.php?state=0"});
}
});
}
私有类后类扩展异步任务{
@凌驾
受保护的Void doInBackground(字符串…参数){
试一试{
最终文本视图输出视图=(文本视图)findViewById(R.id.postOutput);
URL=新URL(参数[0]);
HttpsURLConnection连接=(HttpsURLConnection)url.openConnection();
字符串url参数=”;
connection.setRequestMethod(“POST”);
connection.setRequestProperty(“ACCEPT-LANGUAGE”,“en-US,en;0.5”);
最后一个字符串basicAuth=“Basic”+Base64.encodeToString(“用户:PASSWORD.getBytes(),Base64.URL\u SAFE);
setRequestProperty(“授权”,basicAuth);
connection.setDoOutput(真);
DataOutputStream数据流=新的DataOutputStream(connection.getOutputStream());
dStream.writeBytes(urlParameters);
dStream.flush();
dStream.close();
int responseCode=connection.getResponseCode();
最终StringBuilder输出=新StringBuilder(“请求URL”+URL);
append(System.getProperty(“line.separator”)+“请求参数”+urlParameters);
output.append(System.getProperty(“line.separator”)+“响应代码”+响应代码);
BufferedReader br=新的BufferedReader(新的InputStreamReader(connection.getInputStream());
字符串行=”;
最终StringBuilder responseOutput=新StringBuilder();
而((line=br.readLine())!=null){
响应输出追加(行);
}
br.close();
output.append(System.getProperty(“line.separator”)+“Response”+System.getProperty(“line.separator”)+System.getProperty(“li
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()
}
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;
}
}