package com.jiuqi.njt.data; import java.io.IOException; import java.net.Socket; import java.net.UnknownHostException; import java.security.KeyManagementException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.UnrecoverableKeyException; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.http.client.HttpClient; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; public class CustomerHttpClient { private static HttpClient httpClient = null; private CustomerHttpClient(){} public static HttpClient getImageHttpClient(){ KeyStore trustStore; try { trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory socketFactory = new SSLSocketFactoryEx(trustStore); socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("https", socketFactory, 443)); registry.register(new Scheme("http",PlainSocketFactory.getSocketFactory (), 80)); DefaultHttpClient client = new DefaultHttpClient(); ClientConnectionManager mgr = new ThreadSafeClientConnManager(client.getParams(), registry); HttpClient imageHttpClient = new DefaultHttpClient(mgr, client.getParams()); return imageHttpClient; } catch (Exception e) { e.printStackTrace(); } return null; } public static synchronized HttpClient getHttpClient() { if(httpClient == null){ KeyStore trustStore; try { trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory socketFactory = new SSLSocketFactoryEx(trustStore); socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("https", socketFactory, 443)); registry.register(new Scheme("http",PlainSocketFactory.getSocketFactory (), 80)); DefaultHttpClient client = new DefaultHttpClient(); ClientConnectionManager mgr = new ThreadSafeClientConnManager(client.getParams(), registry); httpClient = new DefaultHttpClient(mgr, client.getParams()); } catch (Exception e) { e.printStackTrace(); } } return httpClient; } } class SSLSocketFactoryEx extends SSLSocketFactory { SSLContext sslContext = SSLContext.getInstance("TLS"); public SSLSocketFactoryEx(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException,KeyStoreException, UnrecoverableKeyException { super(truststore); TrustManager tm = new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() {return null;} public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)throws java.security.cert.CertificateException {} public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)throws java.security.cert.CertificateException {} }; sslContext.init(null, new TrustManager[] { tm }, null); } @Override public Socket createSocket(Socket socket, String host, int port,boolean autoClose) throws IOException, UnknownHostException { return sslContext.getSocketFactory().createSocket(socket, host, port,autoClose); } @Override public Socket createSocket() throws IOException { return sslContext.getSocketFactory().createSocket(); } }