我似乎无法弄清楚如何让这段代码正常工作。难道我做错了什么?该程序在Record [] records = lookup.run()上失败;线。
import java.util.Iterator;
import org.xbill.DNS.ARecord;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.TXTRecord;
import org.xbill.DNS.TextParseException;
import org.xbill.DNS.Type;
Lookup lookup;
try {
lookup = new Lookup("google.com", Type.ANY);
Record[] records = lookup.run();
if (lookup.getResult() == Lookup.SUCCESSFUL) {
String responseMessage = null;
String listingType = null;
for (int i = 0; i < records.length; i++) {
if (records[i] instanceof TXTRecord) {
TXTRecord txt = (TXTRecord) records[i];
for (Iterator j = txt.getStrings().iterator(); j.hasNext();) {
responseMessage += (String) j.next();
}
} else if (records[i] instanceof ARecord) {
listingType = ((ARecord) records[i]).getAddress()
.getHostAddress();
}
}
}
}catch (TextParseException e) {
e.printStackTrace();
}
答案 0 :(得分:1)
你是什么意思不起作用?请按先前的要求粘贴任何异常跟踪。我能够很好地运行代码,我将输出添加到记录中以便查看结果。这是代码:
import java.util.Iterator;
import org.xbill.DNS.ARecord;
import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.TXTRecord;
import org.xbill.DNS.TextParseException;
import org.xbill.DNS.Type;
public class DNS {
public static void main(String[] args) {
try {
Lookup lookup = new Lookup("google.com", Type.ANY);
Record[] records = lookup.run();
if (lookup.getResult() == Lookup.SUCCESSFUL) {
String responseMessage = null;
String listingType = null;
for (int i = 0; i < records.length; i++) {
if (records[i] instanceof TXTRecord) {
TXTRecord txt = (TXTRecord) records[i];
for (Iterator j = txt.getStrings().iterator(); j
.hasNext();) {
responseMessage += (String) j.next();
}
System.out.println("TXRecord " + responseMessage);
} else if (records[i] instanceof ARecord) {
listingType = ((ARecord) records[i]).getAddress()
.getHostAddress();
System.out.println("ARecord address : " + listingType);
}
}
}
} catch (TextParseException e) {
e.printStackTrace();
}
}
}
这是输出:
ARecord address : 74.125.224.49
ARecord address : 74.125.224.51
ARecord address : 74.125.224.52
ARecord address : 74.125.224.48
ARecord address : 74.125.224.50
TXRecord nullv=spf1 include:_netblocks.google.com ip4:216.73.93.70/31 ip4:216.73.93.72/31 ~all