https信任所有证书

public static class WebClientDevWrapper {  
        public static org.apache.http.client.HttpClient wrapClient(
            org.apache.http.client.HttpClient base) {  
            try {  
                SSLContext ctx = SSLContext.getInstance("TLS");  
                X509TrustManager tm = new X509TrustManager() {  
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {  
                        return null;  
                    }  
                    public void checkClientTrusted(X509Certificate[] arg0, String arg1) 
                    throws CertificateException {}  
                    throws CertificateException {}  
                    @Override  
                    public void checkClientTrusted(  
                            java.security.cert.X509Certificate[] chain,  
                            String authType)  
                            throws java.security.cert.CertificateException {  
                        // TODO Auto-generated method stub  

                    }  
                    @Override  
                    public void checkServerTrusted(  
                            java.security.cert.X509Certificate[] chain,  
                            String authType)  
                            throws java.security.cert.CertificateException {  
                        // TODO Auto-generated method stub  

                    }  
                };  
                ctx.init(null, new TrustManager[] { tm }, null);  
                org.apache.http.conn.ssl.SSLSocketFactory ssf = 
                        new org.apache.http.conn.ssl.SSLSocketFactory(ctx, org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
                SchemeRegistry registry = new SchemeRegistry();  
                registry.register(new Scheme("https", 443, ssf));  
                ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(registry);  
                return new DefaultHttpClient(mgr, base.getParams());  
            } catch (Exception ex) {  
                ex.printStackTrace();  
                return null;  
            }  
        }  

    }

使用

HttpClient httpClient = new DefaultHttpClient();
httpClient = WebClientDevWrapper.wrapClient(httpClient);//信任所有证书  
HttpPost httpPost = new HttpPost(bean.getReconUrl());

你可能感兴趣的:(java)