Announcement

Collapse
No announcement yet.

Abfragen eines SSL-Servers

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Abfragen eines SSL-Servers

    Hi zusammen,

    habe ein spezielles Problem (oder sehe den Wald vor lauter Bäumen nicht):
    Ich möchte etwas implementieren, daß einfach die Verfügbarkeit eines SSL-Servers abprüft. Bei meiner bisherigen Umsetzung bekomme ich aber eine Exception:<BR><BR>
    javax.net.ssl.SSLException: untrusted server cert chain<BR><BR>
    Wo leigt mein Fehler, bzw. was habe ich vergessen?

    Wie habe ich es angestellt:<BR><BR>
    <PRE>
    import java.net.*;
    import javax.net.*;
    import javax.net.ssl.*;
    import javax.security.cert.*;
    import com.sun.net.ssl.*;

    public class HttpCheck
    {
    public static void check(String protocol, String host, String port ) throws Exception
    { String p = protocol.trim();
    String h = host.trim();
    String pt = port.trim();
    boolean sslused = false;
    int i;
    if (p.equalsIgnoreCase("https"))
    { sslused = true;
    setHttps();}

    String url= p + "://" + h + ":" + pt;
    try
    { URL u = new URL(url);
    if (sslused) {
    HttpsURLConnection hp = (HttpsURLConnection)u.openConnection();
    i = hp.getResponseCode();
    System.out.println(i); }
    else
    { HttpURLConnection hp = (HttpURLConnection)u.openConnection();
    i = hp.getResponseCode();
    System.out.println(i);}
    }
    catch (Exception ex)
    { System.out.println("Fehler beim Connect zu " + url);
    ex.printStackTrace(); }
    }

    public static void setHttps() {
    try {
    // dynamic registration of JSSE provider for https
    java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    // need to be set for https
    System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
    }
    catch(Exception e) {
    System.out.println("Exception during registration of https protocol: " + e.getMessage() );
    }
    }
    }
    </PRE>
Working...
X