All Products
Search
Document Center

HTTPDNS:Java SDK integration guide

Last Updated:Jun 22, 2026

Integrate HTTPDNS into your Java application using the Java SDK. For integration principles, see Client integration overview.

Important

For Android applications, see the Android SDK integration guide. The Java SDK does not support Android-specific features, such as a local persistent cache or automatic parsing upon network changes.

1. Getting started

1.1. Activate the service

See Quick Start to activate HTTPDNS.

1.2. Get configurations

Obtain your AccountId, SecretKey, and AESSecretKey from the Developer Configurations page in the EMAS console. This information is required to initialize the SDK. For more information, see Developer configurations.

2. Installation and configuration

Configure your project using either Gradle or Maven, depending on your build tool.

2.1. Configure the repository address

The SDK is published to the Alibaba Cloud Maven repository. Add the repository address.

Gradle

Add the following code to your settings.gradle or build.gradle file:

repositories {
    maven { 
      url 'https://maven.aliyun.com/nexus/content/repositories/releases/' 
    }
}

Maven

Add the following code to your pom.xml file:

<repositories>
    <repository>
        <id>aliyun</id>
        <url>https://maven.aliyun.com/nexus/content/repositories/releases/</url>
    </repository>
</repositories>

2.2. Add the SDK dependency

Gradle

In your build.gradle file, add the following code to the dependencies block:

dependencies {
    implementation 'com.aliyun.ams:alicloud-java-httpdns:1.0.0'
}

Maven

In your pom.xml file, add the following code to the <dependencies> block:

<dependency>
    <groupId>com.aliyun.ams</groupId>
    <artifactId>alicloud-java-httpdns</artifactId>
    <version>1.0.0</version>
</dependency>

3. Configuration and usage

3.1. Initialize configurations

After your application starts, initialize the SDK with your AccountId, SecretKey, and feature toggles to enable HTTPDNS:

import com.alibaba.sdk.java.httpdns.HttpDnsClient;
import com.alibaba.sdk.java.httpdns.InitConfig;
import com.alibaba.sdk.java.httpdns.RequestIpType;

// Create a configuration.
InitConfig config = new InitConfig.Builder()
    .setSecretKey("your-secret-key")           // Signing key
    .setEnableHttps(true)                      // Use HTTPS for resolution.
    .setTimeoutMillis(2000)                    // Timeout period in milliseconds.
    .setEnableExpiredIp(true)                  // Allow the use of expired IPs.
    .enableMemoryCache(true)                   // Enable memory cache (enabled by default).
    .build();

// Initialize.
HttpDnsClient.init("your-account-id", config);

// Get the client instance.
HttpDnsClient client = HttpDnsClient.getClient("your-account-id");
Important
  • Enabling HTTPS by setting the setEnableHttps parameter to true incurs additional costs. For more information, see Billing.

  • If you require higher security for domain name information or SDNS parameters, you can set the setAesSecretKey parameter to enable content-layer encryption for resolution requests. This feature incurs additional costs. For more information, see Billing.

3.1.1. Configure logs

During application development, configure HttpDnsLog to output HTTPDNS logs:

import com.alibaba.sdk.java.httpdns.log.HttpDnsLog;
import com.alibaba.sdk.java.httpdns.ILogger;

// Enable log output.
HttpDnsLog.enable(true);

// Set a custom logging interface.
HttpDnsLog.setLogger(new ILogger() {
    @Override
    public void log(String msg) {
        System.out.println("[HTTPDNS] " + msg);
    }
});

3.1.2. Record the SessionId

While your application is running, retrieve the sessionId and record it in your data ingestion system to help troubleshoot online issues:

String sessionId = client.getSessionId();
System.out.println("SessionId = " + sessionId);

3.2. Domain name resolution

3.2.1 Pre-parsing

Configure pre-resolution for frequently accessed domain names so the SDK resolves them in advance and reduces first-request latency:

List<String> hosts = Arrays.asList("www.aliyun.com", "www.example.com");
client.setPreResolveHosts(hosts, RequestIpType.both);

3.2.2. Resolve a domain name

Call the resolution method to retrieve IP addresses for a domain name:

HTTPDNSResult result = client.getHttpDnsResultForHostSyncNonBlocking(
    "www.aliyun.com", 
    RequestIpType.both
);

if (result != null && result.getIps() != null) {
    String[] ipv4List = result.getIps();
}

if (result != null && result.getIpv6s() != null) {
    String[] ipv6List = result.getIpv6s();
}

4. What to do next

After you retrieve an IP address, apply it to your network requests. Depending on the network library you use, see Best practices for HTTPDNS and OkHttp in Java or Best practices for HTTPDNS and Apache HttpClient in Java.

For advanced configuration and features such as authentication requests, see Java SDK API.

5. Notes

  1. Compilation fails after an SDK upgrade

    If compilation fails after you upgrade the SDK, the SDK API may have changed. See the Java SDK API and use the new replacement interfaces.

  2. Write fallback code

    You must include fallback code. If HTTPDNS fails to return the expected result, your application must fall back to using the local DNS for domain name resolution.

  3. Record the IP address and sessionId from HTTPDNS

    Record the IP address and sessionId from HTTPDNS in your logs for troubleshooting. For more information, see How to use the session tracking solution to troubleshoot resolution errors.