CanonicalHostnameVerifier.java

package fr.umlv.ji.tcp.client.https;
import javax.net.ssl.*;
import java.net.InetAddress;
import java.security.cert.X509Certificate;
public class CanonicalHostnameVerifier implements HostnameVerifier {
  public boolean verify(String hostname, SSLSession session) {
    try {
      String canonicalName =
    InetAddress.getByName(hostname).getCanonicalHostName();
      X509Certificate cert =
    (X509Certificate)session.getPeerCertificates()[0];
      String certDN = cert.getSubjectDN().getName();
      sun.security.x509.X500Name name =
    new sun.security.x509.X500Name(certDN);
      return canonicalName.equalsIgnoreCase(name.getCommonName());
    } catch (Exception e) {
      return false;
    }
  }
}