code: public List<Certificate> getUrlCerts(URL url) {
  
          SSLSocket socket = null;
  
          TrustManager trm = new X509TrustManager() {
              public X509Certificate[] getAcceptedIssuers() {
                  return null;
              }
  
              public void checkClientTrusted(X509Certificate[] certs, String authType) {
              }
  
              public void checkServerTrusted(X509Certificate[] certs, String authType) {
              }
          };
  
          try {
  
              SSLContext sc = SSLContext.getInstance("SSL");
              sc.init(null, new TrustManager[]{trm}, null);
              SSLSocketFactory factory = sc.getSocketFactory();
              socket = (SSLSocket) factory.createSocket(url.getHost(), (url.getPort() == -1) ? url.getDefaultPort() : url.getPort());
              socket.startHandshake();
              SSLSession session = socket.getSession();
              Certificate[] certArray = session.getPeerCertificates();
              return new ArrayList<Certificate>(Arrays.asList(certArray));
  
          } catch (Exception e){
              e.printStackTrace();
          } finally {
              if (socket != null && socket.isConnected()) {
                  try {
                      socket.close();
                  } catch (IOException ignored) {}
              }
          }
  
          return new ArrayList<Certificate>();
      }
docs: Gets all certificates from an HTTPS endpoint URL
  
  @param url URL to get certificates from
  @return List of certificates retrieves from SSL/TLS endpoint