HttpsURLConnection使用POST请求方法返回空值

时间:2017-07-19 11:07:50

标签: java php android ssl httpsurlconnection

我目前在带有php脚本的linux机器上安装了SSL安全服务器。

我目前有一个Android应用程序,登录页面将与我的服务器建立安全连接,并通过使用php脚本引用数据库中的详细信息来验证用户。如果详细信息正确,则会向我的应用返回特定值。

将网址对象分配到“ https :// mydomain / myscript”时,返回的值为“null”,但是当我将网址对象分配给“ http 时:// mydomain / myscript“返回正确的值,应用程序内的身份验证可以开始。

我有 -

  1. 检查我的图书馆导入,我正在使用“javax.net.ssl.HttpsURLConnection”
  2. 检查我的安全链接“https://mydomain/myscript”是否可供开放世界访问
  3. 正确安装SSL证书并在apache服务器中全面应用
  4. 这是我的Android代码:

    protected String doInBackground(String... params) {
        String login_url = "https://xxx.xxxxx.com/json/json_login.php";
        String method = params[0];
        if(method.equals("login")){
            String database_name = params[1];
            String password_user = params[2];
            try {
                URL url = new URL(login_url);
                HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
                httpsURLConnection.setRequestMethod("POST");
                httpsURLConnection.setDoOutput(true);
                httpsURLConnection.setDoInput(true);
                OutputStream outputStream = httpsURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String data = URLEncoder.encode("database_name","UTF-8")+"="+URLEncoder.encode(database_name,"UTF-8")+"&"+
                        URLEncoder.encode("password_user","UTF-8")+"="+URLEncoder.encode(password_user,"UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpsURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                response = "";
                String line = "";
                while((line = bufferedReader.readLine())!=null){
                    response += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpsURLConnection.disconnect();
                return response;
    
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    
    @Override
        protected void onPostExecute(String result) {
            if(result.equals("Login Failed")){
                Toast.makeText(ctx, "Database password incorrect", Toast.LENGTH_SHORT).show();
            }else{
                //Create shared preferences module called "database" and write result to shared preferences variable "database_name"
                SharedPreferences preferences = ctx.getSharedPreferences("database", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("database_name", result);
                editor.apply();
                Intent intent = new Intent(ctx, MainActivity.class);
                ctx.startActivity(intent);
                ((Activity)ctx).finish();
    }
    

    这是我的PHP代码:

    <?php
    require "../init_accounts.php";
    
    $database_name = $_POST["database_name"];
    $password_user = $_POST["password_user"];
    
    $sql_query = "SELECT database_name FROM xxx_Table WHERE database_name LIKE '$database_name' and password_user LIKE '$password_user';";
    
    $result = mysqli_query($con,$sql_query);
    
    
    if (mysqli_num_rows($result)>0)
    {
        $row = mysqli_fetch_assoc($result);
        $database_name = $row["database_name"];
        echo "$database_name";
    }
    else
    {
        echo "Login Failed" . mysqli_error($con);
    }
    
    ?>
    

    以下是使用HTTPS URL时的调试结果(返回值不正确):

    HTTPS URL, incorrect return value

    以下是使用HTTP URL(正确的返回值)时的调试结果:

    HTTP URL, correct return value

    如果有人能够在正确的方向上发出一些亮点,那就太棒了!提前致谢

    2017年7月19日更新:

    现在已修复此问题,我使用Trusting all certificates using HttpClient over HTTPS来帮助解决问题!

0 个答案:

没有答案