Android Https post代码片段
private static TrustManager myX509TrustManager = new X509TrustManager() { @Override public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } };
URL url = new URL(httpUrl); HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); //设置SSLContext SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(null, new TrustManager[]{myX509TrustManager}, null); //设置套接工厂 connection.setSSLSocketFactory(sslcontext.getSocketFactory()); connection.setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String s, SSLSession sslSession) { return true; } }); connection.setRequestMethod("POST"); connection.setConnectTimeout(10000); connection.setReadTimeout(10000); connection.setDoOutput(true); connection.setDoInput(true); ......