I am trying to try catch two cases when the url is http or https and do the respective URLConnections. However when I use the code below, the https connection always throws an IOException, while http connection works.
//proxy, url, are given variables
URL checkUrl = new URL(url);
HttpURLConnection connect = null;
if(checkUrl.getProtocol().toLowerCase().equals("https")) {
connect = (HttpsURLConnection) new URL(url).openConnection(proxy);
}else {
connect = (HttpURLConnection) new URL(url).openConnection(proxy);
}
....
connect.connect();
After the connect() command my program jumps to the catch IOException branch. and I get javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
When I debug it, my connect variable gets a value of "HttpsURLConnectionImpl (id=48)" after it does the httpsurlconnection openConnection() command.