简体   繁体   English

如何使用Java从DNS检索多个TXT记录?

[英]How do I retrieve multiple TXT records from DNS with Java?

I have a domain with multiple TXT records. 我有一个包含多个TXT记录的域名。 Dig shows all of them. Dig显示了所有这些。 The nameserver returns them in a non-deterministic order. 名称服务器以非确定性顺序返回它们。 Trying to retrieve these records with the javax.naming.directory classes only every results in the first name returned by the nameserver — sometimes it's one, sometimes another, because the order returned by the nameserver varies. 尝试使用javax.naming.directory类检索这些记录只会导致名称服务器返回的第一个名称 - 有时它是一个,有时是另一个,因为名称服务器返回的顺序不同。

Here's a code fragment: 这是一段代码片段:

Hashtable<String, String> env = new Hashtable<String, String>();
env.put("java.naming.factory.initial",
            "com.sun.jndi.dns.DnsContextFactory");
DirContext dirContext = new InitialDirContext(env);
Attributes attrs = dirContext.getAttributes(name, new String[] { "TXT" });

At this point, attrs only ever contains one Attribute . 此时, attrs只包含一个Attribute Is this expected behaviour? 这是预期的行为吗? How can I make Java retrieve all the TXT records? 如何让Java检索所有TXT记录?

In my own tests, the (single) returned attribute contains both of the TXT records in the domain I tried: 在我自己的测试中,(单个)返回属性包含我尝试的域中的两个TXT记录:

Attributes attrs = dirContext.getAttributes("paypal.com", new String[] { "TXT" });
Attribute txt = attrs.get("TXT");
NamingEnumeration e = txt.getAll();
while (e.hasMore()) {
     System.out.println(e.next());
}

If that isn't working for you, the dnsjava library would certainly allow you to obtain all of the records. 如果这对您不起作用, dnsjava库肯定会允许您获取所有记录。

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

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