代码之家  ›  专栏  ›  技术社区  ›  paul

HTTPS主机名错误:应为<sub.domain.com>。这是什么原因?

  •  21
  • paul  · 技术社区  · 15 年前

    我在尝试使用HTTPS连接到服务器时遇到此“HTTPS主机名错误:”错误。我的url看起来像这样

    https://sub.domain.com/tamnode/webapps/app/servlet.
    

        // Create a URLConnection object for a URL
        URL url = new URL(requestedURL);
        HttpURLConnection.setFollowRedirects(false);
    
        // connect
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("User-Agent", USER_AGENT); //$NON-NLS-1$
    
        OutputStreamWriter wr = new OutputStreamWriter(connection
                .getOutputStream());
    

    但随后会出现一个错误

    IOException: HTTPS hostname wrong:  should be <sub.domain.com>. 
        at sun.net.www.protocol.https.HttpsClient.checkURLSpoofing
        ....
    

    这是一段在过去有效但不再有效的代码。系统架构已经发生了一些变化,但我需要在接触负责人之前获得更多数据。

    什么会导致此错误?我可以关闭URL欺骗检查吗?

    4 回复  |  直到 15 年前
        1
  •  20
  •   cletus    15 年前

    看起来domain.com的SSL证书已授予sub.domain.com。或者,更有可能的是,在未更新SSL证书的情况下,将原来的domain.com重命名为sub.domain.com。

        2
  •  13
  •   Community CDub    7 年前

    cletus 可能的原因是对的。

    HostnameVerifier 在比“通常”更多的情况下,这会返回真实值。

    您可以通过调用 setHostnameVerifier

    这个答案是“受启发的”: http://www.java-samples.com/showtutorial.php?tutorialid=211

    http://www.google.com/search?q=https+hostname+wrong+should+be

    还有一个注意事项:做这件事之前要三思。您将在客户端和服务器组件之间的安全性中创建一个可利用的弱点。

        3
  •  9
  •   Muhammad Imran Tariq    12 年前

    我得到了这个例外- java.io.IOException: HTTPS hostname wrong: should be <localhost>

    我的解决方案是更改我的自签名证书并使 CN=localhost .

    cn=<domain-name> 您的主机文件可能位于 c:/windows/system32/drivers/etc/。。。

        4
  •  6
  •   svarog Rostyslav    6 年前

    下面的代码解决了我的问题

    static {
        //for localhost testing only
        javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
                new javax.net.ssl.HostnameVerifier() {
    
            @Override
            public boolean verify(String hostname,
                    javax.net.ssl.SSLSession sslSession) {
                if (hostname.equals("your_domain")) {
                    return true;
                }
                return false;
            }
        });
    }
    
        5
  •  0
  •   jaykumarark    5 年前

    使用主机名(dns名称)作为别名。

    前任:

    keytool -import -alias <google.com> -file certificate_one.cer -keystore cacerts
    
        6
  •  0
  •   shanmugam    5 年前

    默认情况下,Java验证证书CN(公共名称)是否与URL中的主机名相同。如果 主机名

        7
  •  0
  •   jegadeesh    5 年前

    这只是“svarog”帖子的另一种选择

    static {
    
        HttpsURLConnection.setDefaultHostnameVerifier((hostname, session) -> hostname.equals("domain name"));
    }