httpclient https忽略证书


import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLContext;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;


public class HttpUtilTemp {

	private static CloseableHttpClient buildSSLCloseableHttpClient()
			throws Exception {
		SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null,
				new TrustStrategy() {
					// 信任所有
					public boolean isTrusted(X509Certificate[] chain,
							String authType) throws CertificateException {
						return true;
					}
				}).build();
		// ALLOW_ALL_HOSTNAME_VERIFIER:这个主机名验证器基本上是关闭主机名验证的,实现的是一个空操作,并且不会抛出javax.net.ssl.SSLException异常。
		SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
				sslContext, new String[] { "TLSv1" }, null,
				SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
		return HttpClients.custom().setSSLSocketFactory(sslsf).build();
	}

	public static void main(String[] args) {
		try {
			CloseableHttpClient buildSSLCloseableHttpClient = buildSSLCloseableHttpClient();
			System.setProperty("jsse.enableSNIExtension", "false");
			HttpGet httpGet = new HttpGet(
					"https://www.cunnar.com/opencloud/api/account/file/download.json?access_token=abc&file_id=8914441");
			HttpResponse response = buildSSLCloseableHttpClient
					.execute(httpGet);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}


	org.apache.httpcomponents
	httpclient
	4.3.6




你可能感兴趣的:(java,https)