本来直接用httpclient 4.2.1的DefaultHttpClient是可以直接请求https接口的,由于未知原因在本地请求接口一点问题都没有,放到服务器就报:javax.net.ssl.SSLException: hostname in certificate didn't match: <xxx.xxx.xx.xxx> != <*.a.xxxxx.com>,证书中的域名是:*.xxxxx.com,在服务器请求就变成了:*.a.xxxxx.com,这个才疏学浅解决不了,只能另辟蹊径忽略证书校验,然后上网搜索怎么解决忽略证书校验,结果要么是以前的版本,导致方法过时,要么以之后的版本没有新的对象,折腾好几天还是没解决(不要问为什么不升级版本,二开。。。。。),最后没办法只能去看httpclient 4.2.1帮助文档,不废话了上代码
//过滤证书验证
TrustStrategy ts = new TrustStrategy() {
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// TODO Auto-generated method stub
return true;
}
};
SSLSocketFactory sf = new SSLSocketFactory(ts, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Scheme https = new Scheme("https", 443, sf);
SchemeRegistry sr = new SchemeRegistry();
sr.register(https);
ClientConnectionManager ccm = new BasicClientConnectionManager(sr);
HttpClient httpClient = new DefaultHttpClient(ccm);//生成httpClient对象