Example #1
0
  private void print_https_cert(HttpsURLConnection con) {

    if (con != null) {

      try {

        System.out.println("Response Code : " + con.getResponseCode());
        System.out.println("Cipher Suite : " + con.getCipherSuite());
        System.out.println("\n");

        Certificate[] certs = con.getServerCertificates();
        for (Certificate cert : certs) {
          System.out.println("Cert Type : " + cert.getType());
          System.out.println("Cert Hash Code : " + cert.hashCode());
          System.out.println("Cert Public Key Algorithm : " + cert.getPublicKey().getAlgorithm());
          System.out.println("Cert Public Key Format : " + cert.getPublicKey().getFormat());
          System.out.println("\n");
        }

      } catch (SSLPeerUnverifiedException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
  private String getHttpsCertificateInformation(HttpsURLConnection con) {
    if (con != null) {
      StringBuilder sb = new StringBuilder();
      try {
        sb.append("\nResponse Code : ");
        sb.append(con.getResponseCode());
        sb.append("\nCipher Suite : ");
        sb.append(con.getCipherSuite());
        sb.append("\n");

        Certificate[] certs = con.getServerCertificates();
        for (Certificate cert : certs) {
          sb.append("\nCert Type : ");
          sb.append(cert.getType());
          sb.append("\nCert Hash Code : ");
          sb.append(cert.hashCode());
          sb.append("\nCert Public Key Algorithm : ");
          sb.append(cert.getPublicKey().getAlgorithm());
          sb.append("\nCert Public Key Format : ");
          sb.append(cert.getPublicKey().getFormat());
          sb.append("\n");
        }

      } catch (IOException e) {
        return "Error while retrieving certificate information: " + e.getMessage();
      }

      return sb.toString();
    }
    return "Given connection was null!";
  }