1. httpclient 4.2.3
1.1 download urlimport java.io.IOException; import java.net.Socket; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocket; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; 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.PoolingClientConnectionManager; import org.apache.http.params.HttpParams; import org.apache.http.util.EntityUtils; public class HttpsClient { public static void main(String[] args) throws Exception { SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[]{new MyTrustManager()}, null); SSLSocketFactory sslSocketFactory = new MySSLSocketFactory(sslContext); SchemeRegistry schemeRegister = new SchemeRegistry(); schemeRegister.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); schemeRegister.register(new Scheme("https", 443, sslSocketFactory)); PoolingClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegister); cm.setMaxTotal(200); // 所有主机的最大并发连接数 cm.setDefaultMaxPerRoute(100); // 单个主机的最大并发连接数 HttpGet get = new HttpGet("https://kyfw.12306.cn/otn/login/init"); HttpClient httpClient = new DefaultHttpClient(cm); HttpResponse response = httpClient.execute(get); String respStr = EntityUtils.toString(response.getEntity()); System.out.println(respStr); } } class MyTrustManager implements X509TrustManager { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } } class MySSLSocketFactory extends SSLSocketFactory { public MySSLSocketFactory(SSLContext sslContext) { super(sslContext, ALLOW_ALL_HOSTNAME_VERIFIER); } @Override public Socket createSocket(HttpParams params) throws IOException { Socket socket = super.createSocket(params); if (socket instanceof SSLSocket) { // Allow TLSv1 protocol only ((SSLSocket)socket).setEnabledProtocols(new String[]{"TLSv1"}); } return socket; } }2. httpclient 4.3.6
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.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.PlainConnectionSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; import org.apache.http.util.EntityUtils; public class HttpsClient { public static void main(String[] args) throws Exception { SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[]{new MyTrustManager()}, null); SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory( sslContext, new String[]{"TLSv1"}, null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); Registry<ConnectionSocketFactory> register = RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", sslSocketFactory).build(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(register); cm.setMaxTotal(200); // 所有主机的最大并发连接数 cm.setDefaultMaxPerRoute(100); // 单个主机的最大并发连接数 HttpGet get = new HttpGet("https://kyfw.12306.cn/otn/login/init"); CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build(); HttpResponse response = httpClient.execute(get); String respStr = EntityUtils.toString(response.getEntity()); System.out.println(respStr); } } class MyTrustManager implements X509TrustManager { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { } @Override public X509Certificate[] getAcceptedIssuers() { return null; } }-- END