HttpsURLConnection和BufferedReader问题

时间:2016-11-02 16:35:10

标签: java php android http httpsurlconnection

我正在尝试为我的Android应用程序创建一个安全的登录页面。

登录程序的概要如下:

  1. 用户输入密码
  2. 打开HttpURLConnection并使用URLEncoder和PHP脚本将密码与sql数据库中的条目进行比较。 (注意它是HTTP连接而不是[安全])
  3. 如果密码无效,它将返回“登录失败”的PHP回显,Android后台任务将相应地响应。
  4. 这一切都很好。然而.....

    我最近在主机服务器上安装了一个证书,使网站成为HTTPS [安全] - 因此我继续在Android代码中将“HttpURLConnection”更改为“Http s URLConnection”以适应认证。

    现在每当我使用有效凭据登录时,它应该返回PHP回显“Login Failed”,它返回null ...这导致后台任务中的if语句变得多余。

    这里是BackgroundTaskLogin.java代码:

    public class BackgroundTaskLogin extends AsyncTask<String,Void,String>{
    
    String response;
    
    Context ctx;
    BackgroundTaskLogin(Context ctx){
        this.ctx = ctx;
    }
    
    
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    
    @Override
    protected String doInBackground(String... params) {
        String login_url = "http://apps02.abbeyfield.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);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.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 = httpURLConnection.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();
                httpURLConnection.disconnect();
                return response;
    
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    
    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
    
    @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 abhandym_users 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);
    }
    
    ?>
    

    在将所有HTTP更改为HTTP S 之前,我故意将代码保持在工作状态 - 这是否可以这样做?

    非常感谢任何解释或建议,谢谢!

    编辑17/01/2017 - 仍然没有这个进展,在httpsURLConnection下的Android应用程序的登录页面仍然无法正常工作:( - 任何人都可以提供一些建议吗? - 对于连接是重要的是https,还是http就足够了?

    更新18/01/2017 - 这是将所有http更改为https:

    时logcat返回的内容
        --------- beginning of crash
    01-18 09:49:46.926 3193-3193/com.example.user.ABHandyM E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.user.ABHandyM, PID: 3193
                                                                             java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
                                                                                 at com.example.user.ABHandyM.BackgroundTaskLogin.onPostExecute(BackgroundTaskLogin.java:90)
                                                                                 at com.example.user.ABHandyM.BackgroundTaskLogin.onPostExecute(BackgroundTaskLogin.java:26)
                                                                                 at android.os.AsyncTask.finish(AsyncTask.java:651)
                                                                                 at android.os.AsyncTask.-wrap1(AsyncTask.java)
                                                                                 at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
    

    不太确定为什么在更改连接类型时它返回null?

0 个答案:

没有答案