Reputation: 530
I have a program that needs to ask a local machine for DNS lookups, but I can not modify the global DNS settings of the host. How would I go about doing this?
Upvotes: 1
Views: 4107
Reputation: 1073
You can try to use dnsjava library
Here is a sample:
private static String resolveDomain(String domain, String serverName) {
String hostName = null;
Lookup lookup;
try {
lookup = new Lookup(domain);
Resolver resolver = new SimpleResolver(serverName);
lookup.setResolver(resolver);
Record recs[] = lookup.run();
if (recs == null) {
return null;
}
for (Record rec : recs) {
hostName = rec.getName().toString();
break;
}
} catch (TextParseException e) {
} catch (NullPointerException e) {
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (hostName == null) {
return null;
} else if (hostName.endsWith(".")) {
hostName = hostName.substring(0, hostName.length() - 1);
}
return hostName;
}
Upvotes: 3