Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中从CNAME获取主机名?_Java_Cname - Fatal编程技术网

如何在Java中从CNAME获取主机名?

如何在Java中从CNAME获取主机名?,java,cname,Java,Cname,如何在Java中获得给定CNAME(规范名称)的服务器主机名 例如,我有CNAME“xyz”,但我想获得关联的主机名“myhost” 我尝试使用java.net.InetAddress.getHostName(),但它只是返回了我给出的CNAME 例如: String myCNAME = "xyz"; InetAddress inetAddress = InetAddress.getByName(myCNAME); System.out.printf("My hostname = '%s'",

如何在Java中获得给定CNAME(规范名称)的服务器主机名

例如,我有CNAME“xyz”,但我想获得关联的主机名“myhost”

我尝试使用
java.net.InetAddress.getHostName()
,但它只是返回了我给出的CNAME

例如:

String myCNAME = "xyz";
InetAddress inetAddress = InetAddress.getByName(myCNAME);
System.out.printf("My hostname = '%s'", inetAddress.getHostName());
My hostname = 'xyz'
String myCNAME = "xyz";
InetAddress inetAddress = InetAddress.getByName(myCNAME);
System.out.printf("My hostname = '%s'", inetAddress.getCanonicalHostName());
My hostname = 'myhost.example.com'
只返回给定的CNAME:

String myCNAME = "xyz";
InetAddress inetAddress = InetAddress.getByName(myCNAME);
System.out.printf("My hostname = '%s'", inetAddress.getHostName());
My hostname = 'xyz'
String myCNAME = "xyz";
InetAddress inetAddress = InetAddress.getByName(myCNAME);
System.out.printf("My hostname = '%s'", inetAddress.getCanonicalHostName());
My hostname = 'myhost.example.com'

相反,我想获取主机名“myhost”。

如果您有CNAME,请使用
java.net.InetAddress.getCanonicalHostName()
获取服务器主机名

示例:

String myCNAME = "xyz";
InetAddress inetAddress = InetAddress.getByName(myCNAME);
System.out.printf("My hostname = '%s'", inetAddress.getHostName());
My hostname = 'xyz'
String myCNAME = "xyz";
InetAddress inetAddress = InetAddress.getByName(myCNAME);
System.out.printf("My hostname = '%s'", inetAddress.getCanonicalHostName());
My hostname = 'myhost.example.com'
输出:

String myCNAME = "xyz";
InetAddress inetAddress = InetAddress.getByName(myCNAME);
System.out.printf("My hostname = '%s'", inetAddress.getHostName());
My hostname = 'xyz'
String myCNAME = "xyz";
InetAddress inetAddress = InetAddress.getByName(myCNAME);
System.out.printf("My hostname = '%s'", inetAddress.getCanonicalHostName());
My hostname = 'myhost.example.com'

注意:这将返回FQDN,因此您还将获得“myhost.example.com”的“example.com”。

Java附带了一个DNS客户端,但遗憾的是,它没有很好的文档记录,当您在web上搜索“Java DNS”之类的内容时,它也不太可见。以下代码段使用Java的内置DNS客户端解析CNAME记录:

Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
InitialDirContext idc = new InitialDirContext(env);
Attributes attrs = idc.getAttributes("something.example.com", new String[]{"CNAME"});
Attribute attr = attrs.get("CNAME");
System.out.println(attr.get());
存在一些(不太好的)文档: (找不到10+的更新链接)

依赖于iNETAdvices的解决方案的缺点是它们不考虑CNEntRead,而是执行查找,然后进行反向查找以从IP返回到主机名,但是反向查找返回的主机名通常不同于CNEAR查找实际上指向的主机名。