API reference for the HTTPDNS Java SDK.
1. Basic configuration operations
1.1 Configure initialization
Initializes HTTPDNS service configuration. Call this before any other API.
Operation definition
static void init(String accountId, InitConfig config)
Class
HttpDnsClient
Parameters
|
Parameter |
Type |
Required |
Description |
|
accountId |
String |
Yes |
Account ID assigned by the system. Obtain from the EMAS console. |
|
config |
InitConfig |
Yes |
Initialization configuration object. |
Code example
InitConfig config = new InitConfig.Builder()
.setSecretKey("your-secret-key")
.setEnableHttps(true)
.setTimeoutMillis(2000)
.setEnableExpiredIp(true)
.build();
HttpDnsClient.init("your-account-id", config);
1.2 Get a service instance
Returns an HTTPDNS client instance for domain name resolution.
Interface Definition
static HttpDnsClient getClient(String accountId)
static HttpDnsClient getClient(String accountId, String secretKey)
Class
HttpDnsClient
Parameters
|
Parameter |
Type |
Required |
Description |
|
accountId |
String |
Yes |
Account ID assigned by the system. |
|
secretKey |
String |
No |
Authentication key. |
Return Description
|
Type |
Description |
|
HttpDnsClient |
An HTTPDNS client instance. |
Code example
HttpDnsClient client = HttpDnsClient.getClient("your-account-id");
1.3 InitConfig configuration items
Use InitConfig.Builder to configure initialization settings.
setSecretKey
Sets the key used to sign requests during HTTPDNS resolution.
After setting this key, the SDK signs all server requests, enabling request authentication and tamper-proofing.
Interface Definition
InitConfig.Builder setSecretKey(String secretKey)
Class
InitConfig.Builder
Parameters
|
Parameter |
Type |
Required |
Description |
|
secretKey |
String |
Yes |
The signing key. |
setAesSecretKey
Sets the key used for encryption during HTTPDNS resolution.
After setting this key, the SDK encrypts request parameters and responses with AES. This enhances security but affects billing. Product Billing.
Interface definition
InitConfig.Builder setAesSecretKey(String aesSecretKey)
Class
InitConfig.Builder
Parameters
|
Parameter |
Type |
Required |
Description |
|
aesSecretKey |
String |
Yes |
The encryption key. |
setEnableHttps
By default, the SDK uses HTTP for resolution requests. Enable this to use HTTPS instead.
HTTPS provides greater security but is billed differently. Product Billing.
HTTPS and AES encryption work at different layers. HTTPS secures transport, but captured packets still expose parameters. AES encrypts at the HTTPDNS service layer, hiding plaintext even from captured packets. Enable both for maximum security.
Interface Definition
InitConfig.Builder setEnableHttps(boolean enableHttps)
Class
InitConfig.Builder
Parameters
|
Parameter |
Type |
Required |
Description |
|
enableHttps |
boolean |
Yes |
`true`: Use HTTPS for resolution. `false`: Use HTTP (default). |
setTimeoutMillis
Sets the resolution timeout. Default: 2000 ms.
Interface Definition
InitConfig.Builder setTimeoutMillis(int timeoutMillis)
Class
InitConfig.Builder
Parameters
|
Parameter |
Type |
Required |
Description |
|
timeoutMillis |
int |
Yes |
Resolution timeout in milliseconds. Default: 2000 ms. Maximum: 5000 ms. |
setEnableExpiredIp
The SDK caches resolution results according to their TTL. When the cache expires and the application requests an IP address:
-
Synchronous non-blocking calls return an empty result when the cache has expired, because the SDK cannot immediately retrieve a new result without blocking. Fall back to LocalDNS in this case.
-
Synchronous or asynchronous calls send a resolution request to the server. The synchronous call blocks until a result is obtained; the asynchronous call delivers the result through a callback.
When set to true, resolution operations return the expired IP address immediately instead of waiting, reducing DNS latency. The SDK simultaneously starts an asynchronous refresh to obtain a new result.
Side effects are minimal, especially for domain names with stable resolution (primary sites, static gateways). If resolution does change, only the first request after expiry uses the stale IP, because the SDK always triggers an immediate async update upon finding an expired entry.
Enabled by default.
When set to true, the SDK returns the expired IP address in real time and still performs an asynchronous update to retrieve the latest IP information.
Interface Definition
InitConfig.Builder setEnableExpiredIp(boolean enableExpiredIp)
Class
InitConfig.Builder
Parameters
|
Parameter |
Type |
Required |
Description |
|
enableExpiredIp |
boolean |
Yes |
`true`: Return expired IPs while refreshing asynchronously. `false`: Do not return expired IPs. |
enableMemoryCache
Sets whether to enable the memory cache.
Interface Definition
InitConfig.Builder enableMemoryCache(boolean enable)
Class
InitConfig.Builder
Parameters
|
Parameter |
Type |
Required |
Description |
|
enable |
boolean |
Yes |
`true`: Enables the memory cache (default). `false`: Disables the memory cache. |
setRegion
Sets the initial service node for environments outside China, improving resolution efficiency. The SDK uses this node for resolution and subsequent scheduling updates.
Interface Definition
InitConfig.Builder setRegion(Region region)
Class
InitConfig.Builder
Parameters
|
Parameter |
Type |
Required |
Description |
|
region |
Region |
Yes |
Service node region for environments outside China. If not specified, the Chinese mainland node is used. |
setIPRankingList
Sets the IP preference list. When the corresponding domain name is resolved, the SDK performs speed tests on returned IPs and sorts them by availability.
Only the preference sorting of IPv4 addresses is supported.
Interface Definition
InitConfig.Builder setIPRankingList(List<? extends IPRankingBean> ipRankingList)
Class
InitConfig.Builder
Parameters
|
Parameter |
Type |
Required |
Description |
|
ipRankingList |
List<IPRankingBean> |
Yes |
The IP preference configuration list. |
configCacheTtlChanger
Overrides the server-provided TTL for resolution results with a custom value.
Interface Definition
InitConfig.Builder configCacheTtlChanger(CacheTtlChanger changer)
Class
InitConfig.Builder
Parameters
|
Parameter |
Type |
Required |
Description |
|
changer |
CacheTtlChanger |
Yes |
Custom TTL interface implementation. |
setNotUseHttpDnsFilter
Filters specific domain names from HTTPDNS resolution. Filtered domain names return an empty result; fall back to LocalDNS for these.
Interface Definition
InitConfig.Builder setNotUseHttpDnsFilter(NotUseHttpDnsFilter filter)
Class
InitConfig.Builder
Parameters
|
Parameter |
Type |
Required |
Description |
|
filter |
NotUseHttpDnsFilter |
Yes |
Configure the blacklist policy. |
setSdnsGlobalParams
Sets global parameters for custom resolution (SDNS). These do not override per-request extra parameters but are merged with them. Every custom resolution request carries these global parameters.
Interface Definition
InitConfig.Builder setSdnsGlobalParams(Map<String, String> params)
Class
InitConfig.Builder
Parameters
|
Parameter |
Type |
Required |
Description |
|
params |
Map<String, String> |
Yes |
Global parameters for custom resolution. |
2. Domain name resolution operations
2.1 Set domain names for pre-resolution
Pre-resolves frequently used domain names after SDK initialization to reduce latency for subsequent requests. Calling this while the app is running forces an immediate refresh of the specified domain names.
Calling this while the app is running immediately resolves the specified domain names, updates the cache, and forces a new scheduling. This is typically used after business actions that require refreshing domain name resolution.
The SDK limits each batch resolution request to five domain names. Lists exceeding five are submitted in batches automatically.
setPreResolveHosts
Interface Definition
void setPreResolveHosts(List<String> hostList)
void setPreResolveHosts(List<String> hostList, RequestIpType requestIpType)
Class
HttpDnsClient
Parameters
|
Parameter |
Type |
Required |
Description |
|
hostList |
List<String> |
Yes |
Domain names for pre-resolution. Must be pure domain name strings without protocol headers, paths, or ports. Otherwise, resolution exceptions may occur. Wildcard domain names are not supported. |
|
requestIpType |
RequestIpType |
No |
The IP type for pre-resolution. If this parameter is not specified, the default value is `RequestIpType.v4`. |
Code example
List<String> hosts = Arrays.asList("www.example.com", "api.example.com");
client.setPreResolveHosts(hosts, RequestIpType.both);
2.2 Synchronous non-blocking resolution
-
Does not block the current thread but may return an empty result.
-
Queries only the cache. If no result exists or the result has expired, resolution is performed in a worker thread and the cache is updated for subsequent calls.
getHttpDnsResultForHostSyncNonBlocking
Interface definition
HTTPDNSResult getHttpDnsResultForHostSyncNonBlocking(String host, RequestIpType type)
HTTPDNSResult getHttpDnsResultForHostSyncNonBlocking(String host, RequestIpType type, String clientIp)
HTTPDNSResult getHttpDnsResultForHostSyncNonBlocking(String host, RequestIpType type, Map<String, String> params, String cacheKey)
HTTPDNSResult getHttpDnsResultForHostSyncNonBlocking(String host, RequestIpType type, Map<String, String> params, String cacheKey, String clientIp)
Class
HttpDnsClient
Parameters
|
Parameter |
Type |
Required |
Description |
|
host |
String |
Yes |
The domain name to resolve. |
|
type |
RequestIpType |
Yes |
The IP type to resolve. We recommend that you use `RequestIpType.both`. |
|
clientIp |
String |
No |
The client IP address. This is used in server-side scenarios to specify the actual client IP address for more accurate resolution results. |
|
params |
Map<String, String> |
No |
Extra parameters carried with the domain name resolution, corresponding to the `event.parameters` parameter in the server-side custom resolution function. |
|
cacheKey |
String |
No |
The local cache key for the domain name. When using SDNS, you must pass a non-null value. If changes to extra parameters require re-resolution from the server, you need to change the `cacheKey`. |
Return value
|
Type |
Description |
|
HTTPDNSResult |
The resolution result. |
Code example
HTTPDNSResult result = client.getHttpDnsResultForHostSyncNonBlocking(
"www.example.com",
RequestIpType.both
);
if (result != null && result.getIps() != null && result.getIps().length > 0) {
String ip = result.getIps()[0];
}
2.3 Synchronous resolution
-
Blocks the current thread until a valid resolution result is returned.
-
First queries the cache. If a usable result exists, it is returned immediately. Otherwise, blocks until resolution completes or the timeout is reached.
The total time for a synchronous resolution operation is controlled by the timeout configuration. If the resolution is not successful within the timeout period, an empty resolution result is returned immediately.
getHttpDnsResultForHostSync
Interface Definition
HTTPDNSResult getHttpDnsResultForHostSync(String host, RequestIpType type)
HTTPDNSResult getHttpDnsResultForHostSync(String host, RequestIpType type, String clientIp)
HTTPDNSResult getHttpDnsResultForHostSync(String host, RequestIpType type, Map<String, String> params, String cacheKey)
HTTPDNSResult getHttpDnsResultForHostSync(String host, RequestIpType type, Map<String, String> params, String cacheKey, String clientIp)
Class
HttpDnsClient
Parameters
|
Parameter |
Type |
Required |
Description |
|
host |
String |
Yes |
The domain name to resolve. |
|
type |
RequestIpType |
Yes |
The IP type to resolve. We recommend that you use `RequestIpType.both`. |
|
clientIp |
String |
No |
The client IP address. This is used in server-side scenarios to specify the actual client IP address. |
|
params |
Map<String, String> |
No |
Extra parameters carried with the domain name resolution, corresponding to the `event.parameters` parameter in the server-side custom resolution function. |
|
cacheKey |
String |
No |
The local cache key for the domain name. When using SDNS, you must pass a non-null value. If changes to extra parameters require re-resolution from the server, you need to change the `cacheKey`. |
Return value
|
Type |
Description |
|
HTTPDNSResult |
The resolution result. |
Code example
HTTPDNSResult result = client.getHttpDnsResultForHostSync(
"www.aliyun.com",
RequestIpType.both
);
2.4 Asynchronous resolution
-
Does not block the current thread. The resolution result is returned through a callback.
-
First queries the cache. If a usable result exists, it is returned immediately through the callback. Otherwise, resolution is performed in a worker thread and the result is delivered after completion or timeout.
The total time for an asynchronous resolution operation is controlled by the timeout configuration. If the resolution is not successful within the timeout period, an empty resolution result is returned immediately through the callback.
getHttpDnsResultForHostAsync
Interface Definition
void getHttpDnsResultForHostAsync(String host, RequestIpType type, HttpDnsCallback callback)
void getHttpDnsResultForHostAsync(String host, RequestIpType type, String clientIp, HttpDnsCallback callback)
void getHttpDnsResultForHostAsync(String host, RequestIpType type, Map<String, String> params, String cacheKey, HttpDnsCallback callback)
void getHttpDnsResultForHostAsync(String host, RequestIpType type, Map<String, String> params, String cacheKey, String clientIp, HttpDnsCallback callback)
Class
HttpDnsClient
Parameters
|
Parameter |
Type |
Required |
Description |
|
host |
String |
Yes |
The domain name to resolve. |
|
type |
RequestIpType |
Yes |
The IP type to resolve. We recommend that you use `RequestIpType.both`. |
|
callback |
HttpDnsCallback |
Yes |
The callback operation for the domain name resolution result. |
|
clientIp |
String |
No |
The client IP address, for server-side scenarios. |
|
params |
Map<String, String> |
No |
Extra parameters carried with the domain name resolution, corresponding to the `event.parameters` parameter in the server-side custom resolution function. |
|
cacheKey |
String |
No |
The local cache key for the domain name. When using SDNS, you must pass a non-null value. If changes to extra parameters require re-resolution from the server, you need to change the `cacheKey`. |
Code example
client.getHttpDnsResultForHostAsync("www.example.com", RequestIpType.both,
new HttpDnsCallback() {
@Override
public void onHttpDnsCompleted(HTTPDNSResult result) {
if (result != null && result.getIps() != null) {
String[] ips = result.getIps();
// Use the resolution result.
}
}
});
3. Cache management operations
3.1 Clear the cache for a specific domain name
Clears the local cache so domain name configuration changes take effect in seconds.
For example, assume the domain name is xxx.com and its resolved IP address is ip1. When ip1 is under attack, you need to migrate traffic to ip2. The flow is as follows:
-
First, in the Cloud DNS console, change the IP address for the domain name to ip2. The HTTPDNS server immediately retrieves this change information and purges the ip1 cache for that domain name on the server-side.
-
Manually call this operation to clear the local cache for the xxx.com domain name.
-
The next request after the application clears the cache causes the server-side to re-request the authoritative server to retrieve the latest resolved IP address, ip2. This makes the frontend resolution result immediately take effect as ip2.
cleanHostCache
Interface Definition
void cleanHostCache(ArrayList<String> hosts)
Class
HttpDnsClient
Parameters
|
Parameter |
Type |
Required |
Description |
|
hosts |
ArrayList<String> |
Yes |
An array of host domain names to clear. To clear all data, pass `null` or an empty array. |
Code example
// Clear the cache for a specific domain name.
ArrayList<String> hosts = new ArrayList<>();
hosts.add("www.example.com");
client.cleanHostCache(hosts);
// Clear all caches.
client.cleanHostCache(null);
4. Troubleshooting and tracking operations
4.1 Get the SessionId
Returns the sessionId for tracking resolution issues. Provide this when contacting technical support.
The sessionId is randomly generated, is 12 characters long, and remains unchanged throughout the application lifecycle.
getSessionId
Interface Definition
String getSessionId()
Class
HttpDnsClient
Return value
|
Type |
Description |
|
String |
sessionId |
Code example
String sessionId = client.getSessionId();
System.out.println("SessionId: " + sessionId);
4.2 Get the SDK version number
Retrieves the current SDK version number.
getSdkVersion
Interface Definition
String getSdkVersion()
Class
HttpDnsClient
Return value
|
Type |
Description |
|
String |
The SDK version number. |
Code example
String version = client.getSdkVersion();
System.out.println("SDK Version: " + version);
4.3 Log control
HttpDnsLog.enable
Sets whether to enable logging for HTTPDNS. It is disabled by default.
Interface Definition
static void enable(boolean enable)
Class
HttpDnsLog
Parameters
|
Parameter |
Type |
Required |
Description |
|
enable |
boolean |
Yes |
Whether to print logs. |
Code example
HttpDnsLog.enable(true);
HttpDnsLog.setLogger
You can retrieve the logs that are output by the SDK by setting a log callback class.
Interface Definition
static void setLogger(ILogger logger)
Class
HttpDnsLog
Parameters
|
Parameter |
Type |
Required |
Description |
|
logger |
ILogger |
Yes |
SDK internal logs are returned through the ILogger interface callback, giving you control over log printing and storage. |
Code example
HttpDnsLog.setLogger(new ILogger() {
@Override
public void log(String msg) {
System.out.println("[HTTPDNS] " + msg);
}
});
HttpDnsLog.removeLogger
Deletes the log callback class.
Interface Definition
static void removeLogger(ILogger logger)
Class
HttpDnsLog
Parameters
|
Parameter |
Type |
Required |
Description |
|
logger |
ILogger |
Yes |
The logger that was previously added. |
Code example
HttpDnsLog.removeLogger(myLogger);
5. Other operations
5.1 Set the region
Dynamically updates the region node.
setRegion
Interface Definition
void setRegion(Region region)
Class
HttpDnsClient
Parameters
|
Parameter |
Type |
Required |
Description |
|
region |
Region |
Yes |
The region node. Set a region outside China for service area selection. |
Code example
client.setRegion(Region.HK); // Switch to the China (Hong Kong) node.
5.2 Correct signature time
Corrects the signing timestamp when device time drift occurs. After calling this, the device time is corrected with each network request. If not called, device time is used.
setAuthCurrentTime
Interface Definition
void setAuthCurrentTime(long time)
Class
HttpDnsClient
Parameters
|
Parameter |
Type |
Required |
Description |
|
time |
long |
Yes |
The current UNIX timestamp in milliseconds. |
Code example
HttpDnsClient client = HttpDnsClient.getClient(accountId);
client.setAuthCurrentTime(System.currentTimeMillis());
6. Common data structures
6.1 HTTPDNSResult
The data class for the resolution result returned by a domain name resolution operation.
getHost
Retrieves the corresponding domain name.
Interface Definition
String getHost()
Return value
|
Type |
Description |
|
String |
The domain name. |
getIps
Retrieves the resolved IPv4 addresses.
Interface Definition
String[] getIps()
Return value
|
Type |
Description |
|
String[] |
The resolved IPv4 addresses for the domain name. |
getIpv6s
Retrieves the resolved IPv6 addresses.
Interface Definition
String[] getIpv6s()
Return value
|
Type |
Description |
|
String[] |
The resolved IPv6 addresses for the domain name. |
getExtras
Retrieves extra parameters from the resolution. Only custom resolutions have this data.
Interface Definition
String getExtras()
Return value
|
Type |
Description |
|
String |
The extra parameter string. |
isExpired
Indicates whether the resolution result has expired. Expiration is calculated based on the time the result was obtained and the TTL configured for the domain name.
Interface Definition
boolean isExpired()
Return value
|
Type |
Description |
|
boolean |
`true`: Expired. `false`: Not expired. |
getTtl
Retrieves the Time to Live (TTL) of the resolution result.
Interface Definition
int getTtl()
Return value
|
Type |
Description |
|
int |
The TTL, in seconds. |
6.2 RequestIpType
The enumeration for the requested IP type.
|
Enumeration value |
Description |
|
|
The type to resolve is IPv4. |
|
|
The type to resolve is IPv6. |
|
|
The types to resolve are IPv4 and IPv6. |
Code example
// Resolve both IPv4 and IPv6.
HTTPDNSResult result = client.getHttpDnsResultForHostSyncNonBlocking(
"www.example.com",
RequestIpType.both
);
6.3 Region
The enumeration for the service node region, used for selecting a service region outside China.
|
Enumeration value |
Description |
|
|
The Chinese mainland (default) |
|
|
China (Hong Kong) |
|
|
Singapore |
|
|
Germany |
|
|
United States |
Code example
new InitConfig.Builder().setRegion(Region.SG); // Singapore node
6.4 HttpDnsCallback
The callback interface for the asynchronous domain name resolution operation.
onHttpDnsCompleted
The callback function after the resolution is complete. This function is called for both success and failure.
Interface Definition
void onHttpDnsCompleted(HTTPDNSResult result)
Parameters
|
Parameter |
Type |
Description |
|
result |
HTTPDNSResult |
The resolution result object. |
Code example
client.getHttpDnsResultForHostAsync("www.example.com", RequestIpType.both,
new HttpDnsCallback() {
@Override
public void onHttpDnsCompleted(HTTPDNSResult result) {
if (result != null && result.getIps() != null) {
// Resolution succeeded.
} else {
// Resolution failed or no result.
}
}
});
6.5 ILogger
The log interface.
log
The log method.
Interface Definition
void log(String msg)
Parameters
|
Parameter |
Type |
Description |
|
msg |
String |
The log. |
Code example
HttpDnsLog.setLogger(new ILogger() {
@Override
public void log(String msg) {
// Custom log processing.
System.out.println("[HTTPDNS] " + msg);
}
});
6.6 CacheTtlChanger
The interface for customizing the TTL of a domain name resolution result.
changeCacheTtl
Customizes the TTL.
Interface Definition
int changeCacheTtl(String host, RequestIpType type, int ttl)
Parameters
|
Parameter |
Type |
Description |
|
host |
String |
The domain name. |
|
type |
RequestIpType |
The IP type. |
|
ttl |
int |
The original TTL. |
Response Description
|
Type |
Description |
|
int |
The customized TTL. If no customization is needed, return the original TTL. |
Code example
new InitConfig.Builder()
.configCacheTtlChanger(new CacheTtlChanger() {
@Override
public int changeCacheTtl(String host, RequestIpType type, int ttl) {
if ("www.aliyun.com".equals(host)) {
return ttl * 10;
}
return ttl;
}
});
6.7 NotUseHttpDnsFilter
The interface for filtering domain names that should not be resolved by HTTPDNS.
notUseHttpDns
Configures domain names that should not be resolved by HTTPDNS.
Interface Definition
boolean notUseHttpDns(String hostName)
Parameters
|
Parameter |
Type |
Description |
|
hostName |
String |
The domain name. |
Return value
|
Type |
Description |
|
boolean |
`true`: Exclude this domain from HTTPDNS resolution. `false`: Resolve this domain with HTTPDNS. |
Code example
new InitConfig.Builder()
.setNotUseHttpDnsFilter(new NotUseHttpDnsFilter() {
@Override
public boolean notUseHttpDns(String hostName) {
return "www.aliyun.com".equals(hostName);
}
});
6.8 IPRankingBean
IP preference configuration item. When the corresponding domain name is resolved, the SDK performs speed tests on returned IPs and sorts them by availability.
Constructor
IPRankingBean(String hostName, int port)
Parameters
|
Parameter |
Type |
Description |
|
hostName |
String |
The domain name for IP preference sorting. |
|
port |
int |
The port used to test the speed. |
getHostName
Retrieves the domain name.
Interface Definition
String getHostName()
getPort
Retrieves the port.
Interface Definition
int getPort()
Code example
List<IPRankingBean> list = new ArrayList<>();
list.add(new IPRankingBean("www.example.com", 443));
list.add(new IPRankingBean("api.example.com", 8080));
new InitConfig.Builder()
.setIPRankingList(list);