Https请求javax.net.ssl.SSLException: hostname in certificate didn't match错误

如果直接使用HttpGet(url)会报证书不匹配的异常,在网上查了查,总结如下
直接贴代码

SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, null, null);
SSLSocketFactory sf = new SSLSocketFactory(sslcontext,
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Scheme sch = new Scheme("https", 443, sf);
getHttpClient().getConnectionManager().getSchemeRegistry().register(sch);
HttpGet httpGet = new HttpGet(url);

这样就可以使用了。

你可能感兴趣的:(Https请求javax.net.ssl.SSLException: hostname in certificate didn't match错误)