使用JNDI
事例如下:
/*
 * Created on 2003-10-9
 *
 */
package mytest;import java.util.Hashtable;
import java.util.Enumeration;import javax.naming.*;
import javax.naming.directory.*;/**
 * @author lisong
 *
 */
public class TestMain
{ public static void main(String[] args)
{
try
{
Hashtable env=new Hashtable();
env.put("java.naming.factory.initial","com.sun.jndi.dns.DnsContextFactory");
env.put("java.naming.provider.url","dns://202.102.224.68");

//这里决定可以查询的信息种类,但是dns服务器不保证提供信息
//MX 邮件路有
//A ip地址
//HINFO 主机操作系统信息
//SRV 服务纪录
String dns_attributes[]={"MX","A","HINFO","SRV"};

DirContext ctx =new InitialDirContext(env);

Attributes attrs1=ctx.getAttributes("www.zz.ha.cn",dns_attributes);

if(attrs1==null)
{
System.out.println("Host has none of the specified attributes\n");
}
else
{
for(int z=0;z<dns_attributes.length;z++)
{
Attribute attr=attrs1.get(dns_attributes[z]);
System.out.print(dns_attributes[z]+": ");
if(attr!=null)
{

for(Enumeration vals=attr.getAll();vals.hasMoreElements();)
{
System.out.println(vals.nextElement());
}
}
System.out.println("\n");
}
}
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
}