使用 JNDI 进行DNS, 邮件服务器, 主机信息查找

程序如下:

import java.util.Hashtable;
import java.util.Enumeration; 

import javax.naming.*;
import javax.naming.directory.*; 

public class JNDISearchDNS { 

public static void main(String args[]) {
try {
//Hashtable for environmental information
Hashtable env = new Hashtable(); 

//Specify which class to use for our JNDI provider
env.put("java.naming.factory.initial",
"com.sun.jndi.dns.DnsContextFactory");
env.put("java.naming.provider.url", "dns://129.120.210.252/");// 把这个地址修改为你当前的 DNS 服务器的地址

String dns_attributes[] = {"MX","A","HINFO"}; 

//Get a reference to a directory context
DirContext ctx = new InitialDirContext(env);
Attributes attrs1 = ctx.getAttributes(www.hotmail.com,
dns_attributes); 

if (attrs1 == null) {
System.out.println("host has none of the specified
attributes ");
} else { 

for (int z = 0; z < dns_attributes.length; z++) {
Attribute attr = attrs1.get(dns_attributes[z]); 

if (attr != null) {
System.out.print(dns_attributes[z]+": ");
for (Enumeration vals = attr.getAll
();vals.hasMoreElements();) {
System.out.println(vals.nextElement());
}
} 

System.out.println(" ");
}
}
} catch(Exception e) {
e.printStackTrace();
System.exit(1);
}
}
} 

改程序转自书籍«J2EE 编程指南 1.3 版».

转载请注明:WebLogic Android 博客 » 使用 JNDI 进行DNS, 邮件服务器, 主机信息查找