Monitoring an HTTPS Connection with a URL Class (Java)

I would like to track a simple URL . But when on the https server I get a handshake exception . It can check the state of the https url, how can I use the browser to connect? (without a local certificate). I donโ€™t know how browsers do to get content with https-url, but I would like to do the same. Without the need to store a specific certificate for each server. Monitoring https should be anyone.

try {
    URL u = new URL(row.getUrl());
    String protocol = u.getProtocol();
    if(protocol.equals("https")) {
        HttpsURLConnection hc = (HttpsURLConnection)u.openConnection();
        System.out.println("Response Code: " + hc.getResponseCode());
        hc.disconnect();
    }
    if(protocol.equals("http")) {
    u.openConnection();
    u.getContent();
    }
    System.out.println("url is up.");
} catch (Exception e) {
    (...)
}
+3
source share
1 answer

, SSLContext, TrustManager, . , SSLSocketFactory HttpsURLConnection, , URL. :

TrustManager[] trustEverything = new TrustManager[] {
    new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() { return null; }
        public void checkClientTrusted(X509Certificate[] certs, String authType) { }
        public void checkServerTrusted(X509Certificate[] certs, String authType) { }
    }
};

SSLContext sslctx = SSLContext.getInstance("SSL");
sslctx.init(null, trustEverything, null);

HttpsURLConnection.setDefaultSSLSocketFactory(sslctx.getSocketFactory());

.

@erickson, , , , . - , , , .

+3

Source: https://habr.com/ru/post/1713376/


All Articles