I cant seem to figure out how to get this code to work properly. Am I doing something wrong? the program fails on the Record[] records = lookup.run(); line.
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) {
listingTy开发者_JAVA百科pe = ((ARecord) records[i]).getAddress()
.getHostAddress();
}
}
}
}catch (TextParseException e) {
e.printStackTrace();
}
What do you mean by it does not work? Please paste any exception trace as requested earlier. I was able to run the code just fine, I added output to the records so as to see the result. Here is the code:
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();
}
}
}
And here is the output:
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
精彩评论