¿Deshabilitar la validación del certificado SSL de la conexión HTTPS?
Cuando deseo abrir una conexión de HTTPS consigo la excepción del SSL. ¿Cómo establecer HttpURLConnection de una manera de no ser sensible a esta excepción?
Mi código es:
- WebView con https loadUrl muestra la página en blanco
- ¿Android Volley soporta SSL?
- Charles ssl certificado de descarga no "debido a fallos de red"
- Aceptación de un certificado para HTTPs en Android
- Java.security.cert.CertPathValidatorException: Ancla de confianza para ruta de certificación no encontrada. Android 2.3
private String getData() { String response = null; String connection = "https://www.kamalan.com/"; try { URL url = new URL(connection); Log.i(TAG, "Try to open: " + connection); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); int responseCode = conn.getResponseCode(); Log.i(TAG, "Response code is: " + responseCode); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); if (in != null) { StringBuilder strBuilder = new StringBuilder(); int ch = 0; while ((ch = in.read()) != -1) strBuilder.append((char) ch); // get returned message and show it response = strBuilder.toString(); Log.i("JSON returned by server:", response); } in.close(); } else { Log.e(TAG, "Couldn't open connection in getResepiItems()"); } } catch (SSLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return response; }
- El certificado StartSSL no es de confianza en Firefox y en Android
- ¿Cómo habilito TLSv1.1 + en Xamarin Android (API 16 - 19)?
- Android No se puede verificar el certificado autofirmado sin una conexión previa a Internet
- ¿Cómo puedo solucionar el problema "No se han encontrado certificados: la aplicación Chrome ha solicitado un certificado" Edición de Android / Google Chrome
- Vista web android con certificado de cliente
- Ejemplo de servicio Apache Thrift que utiliza HTTPS, en Python
- ¿El android apoya el tipo del keystore de .jks?
- Volley NoConnectionError: tlsv1 alert fallback inapropiado
Siga el siguiente método, funciona para mí.
URL url = new URL("Your URL"); HttpsURLConnection urlConnection =(HttpsURLConnection) url.openConnection(); urlConnection.setSSLSocketFactory(SSLCertificateSocketFactory.getInsecure(0, null)); urlConnection.setHostnameVerifier(getHostnameVerifier()); InputStream is = urlConnection.getInputStream(); OutputStream os = new FileOutputStream(downloadedFile); byte[] data = new byte[1024]; int count; while ((count = is.read(data)) != -1) { os.write(data, 0, count); } os.flush(); os.close(); is.close();
El siguiente método para establecer el nombre de host
private HostnameVerifier getHostnameVerifier() { HostnameVerifier hostnameVerifier = new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier(); return hv.verify("com.example.com", session); } }; return hostnameVerifier; }