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

本文提供了一种绕过SSL验证的方法,并展示了如何通过信任存储来正确使用SSL连接。这两种方法都涉及到创建自定义的HttpClient实例,用以实现特定的安全配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

避过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);
}
}
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值