繁体   English   中英

Java getHostName错误?

[英]Java getHostName error?

我在Java中使用InetAdress.getHostName()类时遇到问题。 在某些情况下,java类未获得正确的结果。 在这种情况下,我仅收到(如预期的那样,出现错误)IP地址。

这很可能是我们网络的dns配置错误。 但是,如果我将命令行与nslookup一起使用,则会收到正确的答案。

Java是否使用与系统其余部分不同的DNS配置? 谁能解释这可能如何发生? getHostName()是否有替代方法?

getHostName

获取此IP地址的主机名。

getCanonicalHostName

获取此IP地址的标准域名

我看到这是一个很老的问题,但是今天我遇到了同样的问题,看起来好像找到了原因。 问题是,Java(至少openjdk版本为“ 1.8.0_121”)首先查找“ PTR”记录,然后,如果找到任何结果,则Java尝试执行“ A”记录查找,比较返回的“ A” ”,并查询初始IP编号。 而且,如果找不到用于“ A”查询的此类IP,则Java将不会返回“ PTR”查找结果。 他们称其为“防止欺骗”。 这是库资源:

private static String getHostFromNameService(InetAddress addr, boolean check) {
String host = null;
for (NameService nameService : nameServices) {
    try {
        // first lookup the hostname
        host = nameService.getHostByAddr(addr.getAddress());

        /* check to see if calling code is allowed to know
         * the hostname for this IP address, ie, connect to the host
         */
        if (check) {
            SecurityManager sec = System.getSecurityManager();
            if (sec != null) {
                sec.checkConnect(host, -1);
            }
        }

        /* now get all the IP addresses for this hostname,
         * and make sure one of them matches the original IP
         * address. We do this to try and prevent spoofing.
         */

        InetAddress[] arr = InetAddress.getAllByName0(host, check);
        boolean ok = false;

        if(arr != null) {
            for(int i = 0; !ok && i < arr.length; i++) {
                ok = addr.equals(arr[i]);
            }
        }

        //XXX: if it looks a spoof just return the address?
        if (!ok) {
            host = addr.getHostAddress();
            return host;
        }

        break;

我能说什么 省略顽皮的话-什么都没有。 由于此库是从Oracle JRE共享的,因此我们无能为力。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM