Problem
public class UrlConnectionTest {
public static void main(String[] args) throws Exception {
URL url = new URL("http://example.com");
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream(); // occur error
}
}
Caused by: javax.net.ssl.SSLHandshakeException:
java.security.cert.CertificateException: No name matching some_name found
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1611)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:187)
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:181)
......
Caused by: java.security.cert.CertificateException: No name matching some_name found
at sun.security.util.HostnameChecker.matchDNS(HostnameChecker.java:210)
at sun.security.util.HostnameChecker.match(HostnameChecker.java:77)
......
Solution
public class UrlConnectionTest{
public static void main(String[] args) throws Exception {
javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
new javax.net.ssl.HostnameVerifier(){
public boolean verify(String hostname,
javax.net.ssl.SSLSession sslSession) {
// if (hostname.equals("some_name")) { // specific url(hostname) verify
// return true;
// }
// return false;
return true; // all url(hostname) verify
}
});
URL url = new URL("http://example.com");
URLConnection conn = url.openConnection();
InputStream in = conn.getInputStream(); // occur error
}
}