All Products
Search
Document Center

HTTPDNS:Best practices for using HTTPDNS with OkHttp on Android

Last Updated:May 27, 2026

The Android SDK integration process topic covers SDK import, configuration, IP resolution, and integration verification. This topic covers HTTPDNS integration with OkHttp specifically.

1. Background

If you use OkHttp as your network framework on Android, you can integrate HTTPDNS by calling the custom DNS interface that OkHttp provides.

OkHttp is an open-source lightweight Android framework for network requests, developed by Square as an alternative to HttpURLConnection and Apache HttpClient.

By default, OkHttp uses the system DNS service InetAddress for domain name resolution. OkHttp also exposes a custom DNS interface that you can use to integrate HTTPDNS.

Note

This method also works with Retrofit+OkHttp. Pass the configured OkHttpClient to Retrofit.Builder::client(OkHttpClient).

2. Custom DNS interface

OkHttp provides the Dns API to customize DNS resolution:

class OkHttpDns constructor(context: Context): Dns {
    private var mContext: Context

    init {
        mContext = context
    }
    @Throws(UnknownHostException::class)
    override fun lookup(host: String): List<InetAddress> {
        val httpdnsResult: HTTPDNSResult = HttpDns.getService(accountID)
            .getHttpDnsResultForHostSyncNonBlocking(host, RequestIpType.auto)
        val inetAddresses: MutableList<InetAddress> = ArrayList()
        var address: InetAddress
        try {
            if (httpdnsResult.ips != null) {
                // Process IPv4 addresses.
                for (ipv4 in httpdnsResult.ips) {
                    address = InetAddress.getByName(ipv4)
                    inetAddresses.add(address)
                }
            }
            if (httpdnsResult.ipv6s != null) {
                // Process IPv6 addresses.
                for (ipv6 in httpdnsResult.ipv6s) {
                    address = InetAddress.getByName(ipv6)
                    inetAddresses.add(address)
                }
            }
        } catch (e: UnknownHostException) {
        }
        return if (!inetAddresses.isEmpty()) {
            inetAddresses
        } else Dns.SYSTEM.lookup(host)
    }
}
public class OkHttpDns implements Dns {
    private Context mContext;
    public OkHttpDns(Context context) {
        mContext = context;
    }
    @Override
    public List<InetAddress> lookup(String host) throws UnknownHostException {
        HTTPDNSResult httpdnsResult = HttpDns.getService(accountID).getHttpDnsResultForHostSync(host, RequestIpType.both);
        List<InetAddress> inetAddresses = new ArrayList<>();
        InetAddress address;
        try {
            if (httpdnsResult.getIps() != null) {
                // Process IPv4 addresses
                for (String ipv4 : httpdnsResult.getIps()) {
                    address = InetAddress.getByName(ipv4);
                    inetAddresses.add(address);
                }
            }

            if (httpdnsResult.getIpv6s() != null) {
                // Process IPv6 addresses
                for (String ipv6 : httpdnsResult.getIpv6s()) {
                    address = InetAddress.getByName(ipv6);
                    inetAddresses.add(address);
                }
            }
        } catch (UnknownHostException e) {

        }

        if (!inetAddresses.isEmpty()) {
            return inetAddresses;
        }

        return okhttp3.Dns.SYSTEM.lookup(host);
    }
}

During retries, okhttp may try different IP addresses. The lookup() method in the Dns interface returns a List<InetAddress> with multiple IP addresses, which prevents failures caused by a single unavailable IP.

3. Configure OkHttpClient

Create an OkHttpClient and pass in OkHttpDns to replace the default DNS service:

private fun initOkHttp(context: Context) {
    val client = OkHttpClient.Builder() 
        // Configure other initialization settings of OkHttp as required
        .dns(OkHttpDns(context))
        .build()
}
private void initOkHttp(Context context) {
    OkHttpClient client = new OkHttpClient.Builder()
    // Configure other initialization settings of OkHttp as required
    .dns(new OkHttpDns(context))
    .build();
}

The preceding code samples show the complete OkHttp and HTTPDNS integration.

4. Verification

After completing the integration, verify it using hijacking simulation or fault injection testing as described in Verifying the network library integration.

5. Summary

The combination of OkHttp and HTTPDNS provides the following benefits:

  • Integrating HTTPDNS requires only implementing the DNS interface.

  • This solution supports HTTPS, Server Name Indication (SNI), and cookie scenarios without requiring certificate verification or domain name checks.