Android examples for Network:HTTPS SSL
http or https Post
//package com.java2s; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.HashMap; import java.util.Map; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; public class Main { private static Map<String, String> cookieCache = new HashMap<String, String>(); private static SSLContext sslcontext; private static HostnameVerifier hostnameVerifier; public static String httpPost(String url, Map<String, String> params) throws IOException { URL _url = new URL(url); String host = _url.getHost(); HttpURLConnection conn = getHttpURLConnection(_url); conn.setConnectTimeout(3000);//from w w w . ja va2s . c o m conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Accept-Charset", "UTF-8"); String cookie = cookieCache.get(host); if (cookie != null) { conn.setRequestProperty("Cookie", cookie); } conn.setDoInput(true); conn.setDoOutput(true); StringBuilder sb = new StringBuilder(); if (params != null && params.size() > 0) { for (Map.Entry<String, String> entry : params.entrySet()) { sb.append(entry.getKey()).append("=") .append(urlEncode(entry.getValue())).append("&"); } sb.deleteCharAt(sb.lastIndexOf("&")); } OutputStream out = conn.getOutputStream(); out.write(sb.toString().getBytes()); out.flush(); out.close(); sb.delete(0, sb.length()); int responseCode = conn.getResponseCode(); if (responseCode == 200) { BufferedReader reader = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = reader.readLine()) != null) { sb.append(line); } reader.close(); } else { sb = null; } cookie = conn.getHeaderField("Set-Cookie"); if (cookie != null) { cookieCache.put(host, cookie); } conn.disconnect(); return sb != null ? sb.toString() : null; } private static HttpURLConnection getHttpURLConnection(URL url) throws IOException { HttpURLConnection conn; if (url.toString().startsWith("https")) { HttpsURLConnection connection = (HttpsURLConnection) url .openConnection(); connection.setSSLSocketFactory(getSslcontext() .getSocketFactory()); connection.setHostnameVerifier(getHostnameVerifier()); conn = connection; } else { conn = (HttpURLConnection) url.openConnection(); } return conn; } private static String urlEncode(String params) { try { if (params == null) { return ""; } return URLEncoder.encode(params, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return params; } } private static SSLContext getSslcontext() { if (sslcontext == null) { TrustManager trustManager = new X509TrustManager() { @Override public void checkClientTrusted( X509Certificate[] x509Certificates, String s) throws CertificateException { } @Override public void checkServerTrusted( X509Certificate[] x509Certificates, String s) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } }; try { sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(null, new TrustManager[] { trustManager }, new java.security.SecureRandom()); } catch (NoSuchAlgorithmException | KeyManagementException e) { e.printStackTrace(); } } return sslcontext; } private static HostnameVerifier getHostnameVerifier() { if (hostnameVerifier == null) { hostnameVerifier = new HostnameVerifier() { @Override public boolean verify(String s, SSLSession sslSession) { return true; } }; } return hostnameVerifier; } }