避过SSL的方法和使用SSL的方法

避过SSL的方法:

private synchronized static HttpClient getNewHttpClient() {
if (httpClient != null) {
return httpClient;
} else {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);


SSLSocketFactory sf = new MySSLSocketFactory(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 PoolingClientConnectionManager(registry);


httpClient = new DefaultHttpClient(ccm, params);


return httpClient;
} catch (Exception e) {
throw new RuntimeException("init secure client failed", e);
}
}
}


使用SSL的方法:

private synchronized static HttpClient getNewHttpClient() {
if (httpClient != null) {
return httpClient;
} else {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream instream = Thread.currentThread().getContextClassLoader().getResourceAsStream("vtms.keystore");
try {
   trustStore.load(instream, "**********".toCharArray());  // **********代表登录用户名
} finally {
   instream.close();
}


SSLSocketFactory sf = new SSLSocketFactory(trustStore);


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 PoolingClientConnectionManager(registry);


httpClient = new DefaultHttpClient(ccm, params);


return httpClient;
} catch (Exception e) {
throw new RuntimeException("init secure client failed", e);
}
}
}








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