`

HttpClient 如何忽略证书验证访问https - ALLOW_ALL_HOSTNAME_VERIFIER

    博客分类:
  • Java
阅读更多

HttpClient 如何忽略证书验证 - ALLOW_ALL_HOSTNAME_VERIFIER

 

1。设置可以访问HTTPS

 

Function - getNewHttpClient 

 

/** 
	* @Title: getNewHttpClient 
	* @Description: Methods Description
	* @param @return    
	* @return HttpClient 
	* @throws 
	*/ 
	
	private HttpClient getNewHttpClient() {
		try {
			KeyStore trustStore = KeyStore.getInstance(KeyStore
					.getDefaultType());
			trustStore.load(null, null);
			SSLSocketFactory sf = new SSLSocketFactory(trustStore);
			sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
			HttpParams params = new BasicHttpParams();
			HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
			HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
			SchemeRegistry registry = new SchemeRegistry();
			registry.register(new Scheme("http", PlainSocketFactory
					.getSocketFactory(), 80));
			registry.register(new Scheme("https", sf, 443));
			ClientConnectionManager ccm = new ThreadSafeClientConnManager(
					params, registry);
			return new DefaultHttpClient(ccm, params);
		} catch (Exception e) {
			return new DefaultHttpClient();
		}
	}

 

2.忽略证书验证 

 

Class: SSLSocketFactory

 

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 java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.conn.ssl.SSLSocketFactory;


public class SSLSocketFactory extends SSLSocketFactory {
	/** 
	* @Fields sslContext 
	* @Description: Field Description
	*/
	SSLContext sslContext = SSLContext.getInstance("TLS");

	/** 
	* <p>Title: </p> 
	* <p>Description: </p> 
	* @param truststore
	* @throws NoSuchAlgorithmException
	* @throws KeyManagementException
	* @throws KeyStoreException
	* @throws UnrecoverableKeyException 
	*/
	public SSLSocketFactory(KeyStore truststore)
			throws NoSuchAlgorithmException, KeyManagementException,
			KeyStoreException, UnrecoverableKeyException {
		super(truststore);
		TrustManager tm = new X509TrustManager() {
			public void checkClientTrusted(X509Certificate[] chain,
					String authType) throws CertificateException {
			}

			public void checkServerTrusted(X509Certificate[] chain,
					String authType) throws CertificateException {
			}

			public X509Certificate[] getAcceptedIssuers() {
				return null;
			}
		};
		sslContext.init(null, new TrustManager[] { tm }, null);
	}

	/* (non-Javadoc)
	* <p>Title: createSocket</p> 
	* <p>Description: </p> 
	* @param socket
	* @param host
	* @param port
	* @param autoClose
	* @return
	* @throws IOException
	* @throws UnknownHostException 
	* @see org.apache.http.conn.ssl.SSLSocketFactory#createSocket(java.net.Socket, java.lang.String, int, boolean) 
	*/
	@Override
	public Socket createSocket(Socket socket, String host, int port,
			boolean autoClose) throws IOException, UnknownHostException {
		return sslContext.getSocketFactory().createSocket(socket, host, port,
				autoClose);
	}

	/* (non-Javadoc)
	* <p>Title: createSocket</p> 
	* <p>Description: </p> 
	* @return
	* @throws IOException 
	* @see org.apache.http.conn.ssl.SSLSocketFactory#createSocket() 
	*/
	@Override
	public Socket createSocket() throws IOException {
		return sslContext.getSocketFactory().createSocket();
	}
}
 

 

3。调用并用HTTPS访问

 

DefaultHttpClient httpclient = (DefaultHttpClient) getNewHttpClient();
				
		try {
			//Secure Protocol implementation.  
			SSLContext ctx = SSLContext.getInstance("SSL");
			//Implementation of a trust manager for X509 certificates  
			X509TrustManager tm = new X509TrustManager() {

				public void checkClientTrusted(X509Certificate[] xcs,
						String string) throws CertificateException {

				}

				public void checkServerTrusted(X509Certificate[] xcs,
						String string) throws CertificateException {
				}

				public X509Certificate[] getAcceptedIssuers() {
					return null;
				}
			};
			ctx.init(null, new TrustManager[] { tm }, null);
			SSLSocketFactory ssf = new SSLSocketFactory(ctx);
			ClientConnectionManager ccm = httpclient.getConnectionManager();
			//register https protocol in httpclient's scheme registry  
			SchemeRegistry sr = ccm.getSchemeRegistry();
			sr.register(new Scheme("https", 443, ssf));
		} catch (Exception e) {
			e.printStackTrace();
		}

		HttpGet httpGet = new HttpGet(httpGetUrl);
		HttpResponse response = httpclient.execute(httpGet, localContext);
 

 

 

 

0
0
分享到:
评论
1 楼 JetMah 2012-12-18  
可以参考这里:http://stackoverflow.com/questions/2703161/how-to-ignore-ssl-certificate-errors-in-apache-httpclient-4-0

相关推荐

Global site tag (gtag.js) - Google Analytics