有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何解决SSL握手异常

我正在尝试执行以下代码行:

public static String invokeRestService(String request) throws Exception {
        try {

            URL url = new URL("https://XXXXXXX:8403/v1/payment/test/req");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.
            urlConnection.setRequestMethod("POST");
            urlConnection.setRequestProperty("Accept", "application/json");
            urlConnection.setDoOutput(true);
            OutputStreamWriter osw = new OutputStreamWriter(urlConnection.getOutputStream());
            osw.write(request);
            osw.flush();
            osw.close();
            System.out.println(urlConnection.getResponseCode());

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

每当我试图运行上面的代码时,我都会看到下面的错误消息,它说SSL失败了,但当我试图在没有任何授权头的情况下通过postman访问同一个端点URL时,它会按预期工作,并按预期返回200个响应代码。当我试图通过JAVA代码访问端点时,我遇到了这个异常。是否仍然需要设置SSL显式?或者如何使用系统默认SSL

javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No subject alternative names present at sun.security.ssl.Alerts.getSSLException(Alerts.java:192) at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1946)


共 (1) 个答案

  1. # 1 楼答案

    在看了这个链接之后,我正在关注你的问题:https://www.ibm.com/support/knowledgecenter/en/SS9J9E/ioc/ts_liberty_ip.html

    我认为您的DNS记录不正确,这表明:您正在尝试使用不存在的URL执行SSL握手

    你能在浏览器中或使用Curl访问URL吗

    如果不是这样,那么我认为您证书的CN(通用名)与您连接的url的域不匹配