首页 > 解决方案 > 如果我的服务器上没有证书,那么为什么 HttpsUrlConnection 会连接?

问题描述

我刚刚研究出如何在我的服务器上安装 Lets Encrypt 证书,所以当我通过https://.

当我尝试使用以下方法访问 Java 客户端应用程序中的服务器端点时,

        URL httpsURL = new URL(urlStringServerEndPoint);
        sc = SSLContext.getInstance("TLSv1.2");
        sc.init(null, null, new java.security.SecureRandom());
        conn  = (HttpsURLConnection) httpsURL.openConnection();
        conn.setSSLSocketFactory(sc.getSocketFactory());

        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        OutputStream os = conn.getOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
        osw.write(dataString);
        osw.flush();
        osw.close();
        os.close();
        conn.connect();

        if(conn.getResponseCode() != 200)
            throw new MyServerException();

这也有效。

但是 - 如果我从服务器中删除证书并再次运行 Java 客户端,它仍然可以工作。当然它应该抛出一个错误,说没有证书不应该吗?我究竟做错了什么?

标签: javasslcertificatetls1.2

解决方案


推荐阅读