Sample code for 30+ languages & platforms
Java

Use Public DNS Servers (Google and Cloudfare)

See more DNS Examples

Demonstrates how to use Google and Cloudfare nameservers for all DNS lookups within Chilkat.

Note: This example requires Chilkat v9.5.0.96 or later.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Dns;

public class ChilkatExample {

    public static void main(String[] args) {
        // The new Chilkat DNS class is a singleton that allows an applicaton to select
        // the DNS nameservers used throughout Chilkat.

        // "singleton" means that all Chilkat DNS object instances work with a single
        // copy of internal DNS settings that are application wide.

        // Chilkat does DNS lookups internally whenever a domain name is passed to a Chilkat method, or when the domain
        // name is part of a URL passed to a Chilkat method.  It could be in HTTP, Ftp2, SSH, IMAP, Socket, etc.

        // This exampel will show how to use the Cloudfare and Google DNS servers.
        //
        // Google Public DNS:
        //   - Primary DNS: 8.8.8.8
        //   - Secondary DNS: 8.8.4.4
        //   Google Public DNS is known for its speed and reliability. It's operated by Google and is widely used by many internet users.

        // Cloudfare Public DNS:
        //   - Primary DNS: 1.1.1.1
        //   - Secondary DNS: 1.0.0.1
        //   Cloudfare Public DNS is also known for its speed and reliability, and is also widely used.

        Dns dns = new Dns();

        // Indicate our TLS preference:
        // 0: No TLS
        // 1: Prefer TLS if possible
        // 2: Require TLS
        dns.setTlsPref(0);

        // If you choose "No TLS", then all DNS requests will use UDP, or TCP for those requests with large responses.
        // Choosing "No TLS" provides the fastest DNS lookups, and is generally what you're already doing.

        // If you choose "Prefer TLS if possible", then Chilkat will use DNS over TLS for those nameservers that
        // support TLS.

        // If you choose "Require TLS", then Chilkat will ignore nameservers that don't support TLS,
        // and all DNS communications will occur over private and secure TLS connections.

        // When you specify a nameserver to be used, you'll tell Chilkat whether it supports DNS or not.
        // Here' we're setting the nameservers to Google and Cloudfare.
        // They both support TLS, but since we set our TLS preference = 0, Chilkat will not use TLS.
        // (If we set our TLS preference equal to 1 or 2, then Chilkat will use TLS for Google and Cloudfare DNS.)

        // First, make sure we remove all existing nameservers.
        dns.removeAllNameservers();

        // Add the Google and Cloudfare nameservers.
        boolean supportsTls = true;
        dns.addNameserver("1.1.1.1", supportsTls);
        dns.addNameserver("8.8.8.8", supportsTls);
        dns.addNameserver("1.0.0.1", supportsTls);
        dns.addNameserver("8.8.4.4", supportsTls);

        // That's it.  Now all DNS lookups in Chilkat will use the above nameservers.
    }
}