【问题标题】:Access and read html/php page by https request by QML/js behind htaccess apache2 Serverhtaccess apache2 Server后面的QML/js通过https请求访问和读取html/php页面
【发布时间】:2016-06-05 06:32:27
【问题描述】:

我有一个带有 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 页面作为纯字符串读出。


我尝试从here 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");

在 Buttons onClicked-Event 中,但没有响应。


问题 哪种方式可以实现这个任务???

【问题讨论】:

    标签: javascript php html .htaccess qml


    【解决方案1】:

    已解决 Ignoring auto signed SSL certificates 以及 XMLHttpRequest 的一些基础知识。

    处理自签名(或任何)SSL CA:

    将这些包含添加到 main.cpp

    #include <QSslConfiguration>
    #include <QSslSocket>
    

    在 main() 的开头添加这些行

    QSslConfiguration config = QSslConfiguration::defaultConfiguration();
    config.setPeerVerifyMode(QSslSocket::VerifyNone);
    QSslConfiguration::setDefaultConfiguration(config);
    

    通过 htaccess 授权读取文件/响应:

    线 “xhr.setRequestHeader('授权','基本'+ 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-22
      • 1970-01-01
      • 2010-11-04
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      • 2012-12-29
      • 2016-03-30
      相关资源
      最近更新 更多