I need a function which returns true if the certificate of a secure website is signed by a CA. In Android, if you try to connect to a self-signed certificate, it throws an SSLException, in this case I just catch it and return false. You can check the code:
public static boolean isValidCertificate(URL url) throws IOException {
HttpsURLConnection con;
try {
con = (HttpsURLConnection) url.openConnection();
con.connect();
con.disconnect();
return true;
} catch (SSLException e) {
return false;
}
}
My problem is that I want the function to throw an Exception if the site is not avaliable. But I just found out that Android throws the same SSLException in this case, with the same message: "No trusted server certificate".
Is there any way of knowing whether the server is online regardless of whether the certificate is valid or not?
Thanks!