如何处理SSLHandshakeException?

时间:2016-12-12 12:23:28

标签: android android-studio

我正在尝试登录,但它显示了“未找到证书路径的信任锚”的异常。我正在使用Xampp apache服务器。下面是我的backgroundWorker类的代码。我尝试了https://developer.android.com/training/articles/security-ssl.html#UnknownCa中描述的方法,但我没有找到任何文件(“load-der.crt”)。

public class BackgroundWorker extends AsyncTask<String, Void, String> {

    AlertDialog alertDialog;
    Context ctx;
    BackgroundWorker(Context context) {
        this.ctx = context;
    }

    @Override
    protected String doInBackground(String... params) {

        String type = params[0];
        String login_url = "https://192.168.8.104/login.php";
        if (type.equals("login")) {
            try {
                String user_namelogin = params[1];
                String passwordlogin = params[2];
                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 post_data = URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_namelogin, "UTF-8") + "&" +
                        URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(passwordlogin, "UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();

                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
                String result = "";
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    result += line;
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e) {
                Log.e("ERROR", "ERROR IN CONNECTION " + e.toString());
                e.printStackTrace();
            } catch (IOException e) {
                Log.e("ERROR", "ERROR TO OPEN URL CONNECTION " + e.toString());
                e.printStackTrace();
            }

        }

        return null;
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(ctx).create();
        alertDialog.setTitle("Login Status");

    }

    @Override
    protected void onPostExecute(String result) {
        alertDialog.setMessage(result);
        alertDialog.show();

    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}

0 个答案:

没有答案