This document shows you how to integrate HTTPDNS into your Java application using OkHttp.
1. Background
If you use the OkHttp network framework in your Java application, you can easily integrate the HTTPDNS service by implementing OkHttp's custom DNS interface.
OkHttp is an open source project for handling network requests. It is a popular lightweight framework for Java and Android applications, contributed by the mobile payment company Square. As the framework has matured, many developers have adopted it for their applications.
By default, OkHttp uses the system DNS service through the InetAddress class for domain name resolution. OkHttp also exposes an interface that lets you specify a custom DNS service. You can use this interface to integrate HTTPDNS.
2. Implement the custom DNS interface
OkHttp exposes the Dns interface. Implement this interface to create a custom DNS service:
import com.alibaba.sdk.java.httpdns.HTTPDNSResult;
import com.alibaba.sdk.java.httpdns.HttpDnsClient;
import com.alibaba.sdk.java.httpdns.RequestIpType;
import okhttp3.Dns;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
public class OkHttpDns implements Dns {
private final HttpDnsClient httpDnsClient;
public OkHttpDns(HttpDnsClient httpDnsClient) {
this.httpDnsClient = httpDnsClient;
}
@Override
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
HTTPDNSResult result = httpDnsClient.getHttpDnsResultForHostSyncNonBlocking(
hostname,
RequestIpType.both
);
List<InetAddress> inetAddresses = new ArrayList<>();
try {
if (result != null && result.getIps() != null) {
for (String ip : result.getIps()) {
inetAddresses.add(InetAddress.getByName(ip));
}
}
if (result != null && result.getIpv6s() != null) {
for (String ip : result.getIpv6s()) {
inetAddresses.add(InetAddress.getByName(ip));
}
}
} catch (UnknownHostException e) {
// Ignore failures in parsing a single IP address.
}
if (!inetAddresses.isEmpty()) {
return inetAddresses;
}
// Fall back to the system DNS.
return Dns.SYSTEM.lookup(hostname);
}
}OkHttp has a built-in retry mechanism. During a retry, OkHttp may use a different IP address to establish the connection. By returning a list of multiple IP addresses from the lookup() method, you can prevent request failures if a single IP address is unavailable.
3. Configure the OkHttpClient
Create an `OkHttpClient` object and pass your custom DNS service to it:
HttpDnsClient httpDnsClient = HttpDnsClient.getClient(accountId);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.dns(new OkHttpDns(httpDnsClient))
.build();4. Summary
The main advantages of using HTTPDNS with OkHttp are:
Simple implementation: You can integrate the service by implementing the
Dnsinterface.High versatility: Works in scenarios such as HTTPS, Server Name Indication (SNI), and cookie management. No extra steps are needed for certificate validation or domain name verification.
Automatic retries: If you return multiple IP addresses, OkHttp automatically retries connections with different addresses.