This topic describes how to use the HTTPDNS SDK with the Alibaba Cloud OSS SDK to resolve domain names for your Android applications.
Overview
Alibaba Cloud Object Storage Service (OSS) is a secure, cost-effective, and highly reliable cloud storage service that lets you store large amounts of data in the cloud. You can use the HTTPDNS SDK with the OSS SDK to resolve OSS domain names. This practice prevents DNS hijacking and accelerates DNS resolution.
Best practices
This topic provides an example of how to use the Alibaba Cloud OSS SDK for Android with the HTTPDNS SDK and OkHttp to implement custom DNS resolution. For more information about how to integrate the HTTPDNS SDK, see Android SDK Developer Guide.
Customize the DNS interface.
public class OkHttpDns implements Dns {
private static OkHttpDns instance;
private static Object lock = new Object();
private DNSResolver mDNSResolver = DNSResolver.getInstance();
private OkHttpDns() {
}
public static OkHttpDns getInstance() {
if (null == instance) {
synchronized (lock) {
if (instance == null) {
instance = new OkHttpDns();
}
}
}
return instance;
}
@Override
public List<InetAddress> lookup(@NonNull String hostname) throws UnknownHostException {
// Call the API of the HTTPDNS SDK for Android to resolve the domain name.
String ip = mDNSResolver.getIPV4ByHost(hostname);
Log.d(TAG, "mDnsCache IP: " + ip);
if (ip != null) {
// If the IP address is not null, use it to send network requests.
return Arrays.asList(InetAddress.getAllByName(ip));
}
// If null is returned, use the system DNS service to resolve the domain name.
return Dns.SYSTEM.lookup(hostname);
}
}Generate an okHttpClient instance and configure it to OSS.
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
ClientConfiguration conf = new ClientConfiguration();
conf.setConnectionTimeout(15 * 1000); // Connection timeout. Default value: 15 seconds.
conf.setSocketTimeout(15 * 1000); // Socket timeout. Default value: 15 seconds.
conf.setMaxConcurrentRequest(5); // Maximum number of concurrent requests. Default value: 5.
conf.setMaxErrorRetry(2); // The maximum number of retries. Default value: 2.
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.dns(OkHttpDns.getInstance());
// If a custom okHttpClient is set, some ClientConfiguration settings become invalid. You need to manually set them to the builder.
if (conf != null) {
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequests(conf.getMaxConcurrentRequest());
builder.connectTimeout(conf.getConnectionTimeout(), TimeUnit.MILLISECONDS)
.readTimeout(conf.getSocketTimeout(), TimeUnit.MILLISECONDS)
.writeTimeout(conf.getSocketTimeout(), TimeUnit.MILLISECONDS)
.followRedirects(conf.isFollowRedirectsEnable())
.followSslRedirects(conf.isFollowRedirectsEnable())
.dispatcher(dispatcher);
if (conf.getProxyHost() != null && conf.getProxyPort() != 0) {
builder.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(conf.getProxyHost(), conf.getProxyPort())));
}
}
// Only Android OSS SDK version 2.9.12 or later supports the conf.setOkHttpClient() method.
conf.setOkHttpClient(builder.build());
OSS oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider, conf);Initialize the HTTPDNS SDK before initializing the Alibaba Cloud OSS SDK.