Retrieving a DNS response |
To retrieve a response from a DNS request use the Dns#getResponse method passing in a DnsRequest as the argument. This will return a DnsResponse instance which will contain the response provided by the DNS server. A DnsResponse may contain one or more DnsRecord. These records may be retrieved using the DnsResponse#getAnswers method returning a java.util.Enumeration of DnsRecord. DnsRecord is an abstract class. In order to get specific information about a DnsRecord you will need to cast it to it's concrete type.
Example
// create new DNS request DnsRequest request = new DnsRequest("ns1.west.cox.net", "jscape.com");
// set record type to return only MailExchanger records request.setRecordType(Dns.TYPE_MX);
// create new Dns instance Dns dns = new Dns();
// get response DnsResponse response = dns.getResponse(request);
// get records Enumeration answers = response.getAnswers();
// loop thru all records found while (answers.hasMoreElements()) { // get next record DnsRecord record = (DnsRecord) answers.nextElement();
// if is MailExchanger record then print MX hostname if (record instanceof MailExchanger) { System.out.println("Found MX record: " + ((MailExchanger)record).getMX()); } } |