---
title: "使用JNDI进行高级DNS查询"
author: "kyleliu"
date: "2006-06-15T17:19:00Z"
tags: ["JDK", "Java", "SUN"]
slug: "jndi-dns"
draft: false
lastmod: "2006-06-15T17:19:00Z"
id: 1107718
url: "https://kyleliu.iteye.com/blog/1107718"
---

import java.util.\*;  
import javax.naming.directory.\*;

public class DNSLookup{  
public static void main(String\[] args) {  
try {  
final Hashtable env = new Hashtable();  
//设定DNS Service Provider.  
env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFac&lt;wbr&gt;&lt;/wbr&gt;tory");  
DirContext dnsContext = new InitialDirContext(env);  
//设定查询类型, 例如A, MX, AAAA, SRV等.  
final String\[] types = new String\[] {"A"};  
//返回查询结果, sun.com的DNS A记录.  
Attributes attrs = dnsContext.getAttributes("sun.com", types);  
System.out.println(attrs);  
} catch (Exception e) {  
e.printStackTrace();  
}  
}  
}

参考JDK的[DNS Service Provider for the Java Naming Directory InterfaceTM (JNDI)](http://java.sun.com/j2se/1.4.2/docs/guide/jndi/jndi-dns.html)章节