Domain name resolution

Updated at:

This topic describes the domain name resolution API in the HarmonyOS SDK.

Get an HTTPDNS instance

Before you can get an instance, you must complete the basic configurations for HTTPDNS. You can use an HTTPDNS instance to resolve domain names. The following code provides an example:

import { httpdns, HttpDnsError } from '@aliyun/httpdns';

const ACCOUNT_ID = 'Replace this with the Account ID from the Alibaba Cloud HTTPDNS console';

// ************* Get an HTTPDNS instance: begin *************
httpdns.getService(ACCOUNT_ID).then((service) => {
// ************* Get an HTTPDNS instance: end *************
  // Call the service method as needed.
}).catch((e: HttpDnsError) => {
  console.error(`Failed to get the HTTPDNS instance: ${e.code} ${e.message}`);
});

Domain name resolution

Asynchronous resolution

After you get an HTTPDNS instance, call a domain name resolution method. The getHttpDnsResultAsync method is commonly used for asynchronous resolution. The following code provides an example:

import { httpdns, HttpDnsError, HttpDnsResult, IpType } from '@aliyun/httpdns';

const ACCOUNT_ID = 'Replace this with the Account ID from the Alibaba Cloud HTTPDNS console';

const HOST = 'www.aliyun.com';

httpdns.getService(ACCOUNT_ID).then((service) => {
  // ************* Domain name resolution: begin *************
  return service.getHttpDnsResultAsync(HOST, IpType.Auto);
  // ************* Domain name resolution: end *************
}).then((result: HttpDnsResult) => {
  console.log(`Resolved domain name: ${result.host}`);
  console.log(`Resolved IPv4 addresses: ${result.ipv4s?.join(',')}`);
  console.log(`Resolved IPv6 addresses: ${result.ipv6s?.join(',')}`);
}).catch((e: HttpDnsError) => {
  console.error(`Failed: ${e.code} ${e.message}`);
});

getHttpDnsResultAsync is a Promise-based asynchronous method that accepts two parameters:

Parameter

Type

Required

Description

host

string

Yes

The domain name to resolve.

type

IpType

No

The type of IP address. This parameter supports four values. If you do not specify this parameter, the default value is IpType.V4.

  • IpType.V4: Resolves IPv4 addresses.

  • IpType.V6: Resolves IPv6 addresses.

  • IpType.Both: Resolves both IPv4 and IPv6 addresses.

  • IpType.Auto: The system determines the IP address type based on the device's network stack. If the device supports only IPv4, IPv4 addresses are resolved. If the device supports only IPv6 or supports both, both IPv4 and IPv6 addresses are resolved.

The getHttpDnsResultAsync method returns an HttpDnsResult object that contains the following three main parameters:

Parameter

Type

Description

host

string

The resolved domain name.

ipv4s

Array<string>

An array of resolved IPv4 addresses. If no result is found, this parameter is undefined.

ipv6s

Array<string>

An array of resolved IPv6 addresses. If no result is found, this parameter is undefined.

The HTTPDNS instance caches each resolution result in memory. Within the TTL period, the instance does not send repeated resolution requests to the HTTPDNS service for the same domain name.

The default configurations allow the instance to return expired IP addresses. After the TTL expires, the instance first returns the cached resolution result. Then, it sends a resolution request to the HTTPDNS service and updates the memory cache with the new result.

Synchronous resolution

The HTTPDNS instance also provides the getHttpDnsResultSyncNonBlocking synchronous resolution method. The following code shows an example:

import { httpdns, HttpDnsError, HttpDnsResult, IpType } from '@aliyun/httpdns';

const ACCOUNT_ID = 'Replace this with the Account ID from the Alibaba Cloud HTTPDNS console';

const HOST = 'www.aliyun.com';

httpdns.getService(ACCOUNT_ID).then((service) => {
  // ************* Synchronous domain name resolution: begin *************
  const result = service.getHttpDnsResultSyncNonBlocking(HOST, IpType.Auto);
  // ************* Synchronous domain name resolution: end *************
  console.log(`Resolved domain name: ${result.host}`);
  console.log(`Resolved IPv4 addresses: ${result.ipv4s?.join(',')}`);
  console.log(`Resolved IPv6 addresses: ${result.ipv6s?.join(',')}`);
}).catch((e: HttpDnsError) => {
  console.error(`Failed: ${e.code} ${e.message}`);
});

getHttpDnsResultSyncNonBlocking is a synchronous method. This method has the same parameters and return value as getHttpDnsResultAsync.

This method differs from getHttpDnsResultAsync. If no cached result exists, or if the cached result has expired and the configuration does not allow the use of expired IP addresses, this method returns an empty result. The method then sends a resolution request to the HTTPDNS service and updates the memory cache with the new result.

Because HarmonyOS is single-threaded, use the getHttpDnsResultAsync method.

Pre-resolution

HTTPDNS sends resolution requests to the service over HTTP. The first resolution for a domain name is not cached and requires a time-consuming network request. To accelerate network requests, you can pre-resolve major domain names in a batch. This reduces the latency of actual network requests. The following code provides an example of batch resolution:

import { httpdns, HttpDnsError, IpType } from '@aliyun/httpdns';

const ACCOUNT_ID = 'Replace this with the Account ID from the Alibaba Cloud HTTPDNS console';

httpdns.getService(ACCOUNT_ID).then((service) => {
  // ************* Pre-resolution: begin *************
  return service.resolveHosts(['www.aaa.com', 'www.bbb.com', 'www.ccc.com'], IpType.Auto);
  // ************* Pre-resolution: end *************
}).catch((e: HttpDnsError) => {
  console.error(`Failed: ${e.code} ${e.message}`);
});

resolveHosts is an asynchronous method that accepts two parameters:

Parameter

Type

Required

Description

hosts

Array<string>

Yes

A list of domain names to pre-resolve.

type

IpType

No

The type of IP address to resolve. This parameter supports four values. If you do not specify this parameter, the default value is IpType.V4.

  • IpType.V4: Resolves IPv4 addresses.

  • IpType.V6: Resolves IPv6 addresses.

  • IpType.Both: Resolves both IPv4 and IPv6 addresses.

  • IpType.Auto: The system determines the IP address type based on the device's network stack. If the device supports only IPv4, IPv4 addresses are resolved. If the device supports only IPv6 or supports both, both IPv4 and IPv6 addresses are resolved.

After pre-resolution, the HTTPDNS instance stores the resolution results in its memory cache. This allows for fast retrieval during subsequent network requests.