Wednesday, July 13, 2011

java download remote certificate

In one of my project, I need a certificate from the server which provides web service over https. The server can only be visited from a unix box which cannot run firefox, IE…

The java code below can download the certificate. Note, the certificate must be signed by a trusted certification provider, like versign. Otherwise, like self signed certificate, it won’t allow you establish the connection.

URL url = new URL("https://www1.royalbank.com/");
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("Altaproxy.tmi.telus.com", 8080));
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.connect();
Certificate certs[] = con.getServerCertificates();
for (Certificate cert : certs) {
System.out.println(cert.toString());
}
Method getEncoded() of Certificate returns byte[] which allows you save it in a file.

Reference


a blog from internet


java.security.cert.Certificate api


Using proxy in http connection

No comments:

Post a Comment