我有一个程序需要向本地计算机请求DNS查找,但我无法修改主机的全局DNS设置。我该怎么做呢?
答案 0 :(得分:1)
您可以尝试使用dnsjava库
以下是一个示例:
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;
}