JAVA中的DNS查询

JAVA中的DNS查询,java,dns,inetaddress,Java,Dns,Inetaddress,我在Java中玩弄DNS服务-我特别尝试查找所有google.com地址并将其显示在一个数组中,类似于使用nslookup运行查找: nslookup -q=TXT _netblocks.google.com 8.8.8.8 我正在为此使用InetAddress,但不断出现异常错误。由于错误指的是“未知主机”,我认为InetAddress无法读取TXT记录(如果我使用google.com,它可以工作,但不能显示完整的IP范围)。下面是我的代码: InetAddress dnsresult[]

我在Java中玩弄DNS服务-我特别尝试查找所有google.com地址并将其显示在一个数组中,类似于使用nslookup运行查找:

nslookup -q=TXT _netblocks.google.com 8.8.8.8
我正在为此使用
InetAddress
,但不断出现异常错误。由于错误指的是“未知主机”,我认为
InetAddress
无法读取TXT记录(如果我使用google.com,它可以工作,但不能显示完整的IP范围)。下面是我的代码:

InetAddress dnsresult[] = InetAddress.getAllByName("_netblocks.google.com");
            for (int i=0; i<dnsresult.length; i++)
            System.out.println (dnsresult[i]);
InetAddress dnsresult[]=InetAddress.getAllByName(“\u netblocks.google.com”);

对于(int i=0;i您无法查找TXT或其他DNS记录
InetAddress
类。
InetAddress.getAllByName()
仅查找A或AAAA记录


检查您的需要。

InetAddress
不执行此操作,但您可以通过在Java中完成DNS TXT记录查找。

下面是一个示例,它可以完成您正在尝试执行的操作:

Attribute attr = new InitialDirContext().getAttributes("dns:_netblocks.google.com", new String[] {"TXT"}).get("TXT");
System.out.println("attr.get() = " + attr.get());
System.out.println("attr.getAll() = " + Collections.list(attr.getAll()));

如果要使用自定义dns服务器,请使用“dns://1.1/_netblocks.google.com“相反。

这个问题可能不仅仅与Java有关,因为我的
ping
也无法解决
\u netblocks.google.com
。谢谢Zhedar,我知道:)我添加了这个来说明我在尝试做什么。如果我使用“google.com”作为我的论据,它确实会弹出一个IP。但这是一个记录。我需要抓取TXT记录。