SSLSocket ignoriert nicht übereinstimmende Domänen

Ich lese vom SSL-Socket, aber der Host stimmt nicht mit dem Zertifikat überein (z. B.host = "localhost"). Ich würde die Ausnahme erwarten, aber der folgende Code spricht problemlos mit Remote-Server.

try (
    final Socket socket = SSLSocketFactory.getDefault().createSocket(host, port);
    final OutputStream os = socket.getOutputStream();
    final InputStream is = socket.getInputStream()) {

    os.write(("HEAD / HTTP/1.1\r\nHost: " + host + "\r\nConnection: close\r\n\r\n").getBytes());
    os.flush();

    final byte[] bytes = new byte[1024];
    int n;
    while ((n = is.read(bytes)) != -1) {
        System.out.print(new String(bytes, 0, n));
    }
    System.out.println();
} catch (final IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Deshalb habe ich einen anderen Ansatz versucht:

try {
    final HttpURLConnection conn = (HttpURLConnection) new URL("https://" + host + ":" + port + "/").openConnection();

    try (InputStream is = conn.getInputStream()) {
        IOUtils.copy(is, System.out);
    } catch (final IOException e1) {
        try (InputStream es = conn.getErrorStream()) {
            if (es != null) {
                IOUtils.copy(es, System.out);
            }
        }
    }
} catch (final IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Leider bekomme ich immer noch keine SSL-Ausnahme, nur WARN in den Logs:2013-07-31 16:02:27,182 WARN nio - javax.net.ssl.SSLException: Received fatal alert: certificate_unknown

Wie erhalte ich die SSL-Ausnahme, wenn das Zertifikat nicht übereinstimmt?

Antworten auf die Frage(2)

Ihre Antwort auf die Frage