All Products
Search
Document Center

HTTPDNS:Switch regions

Last Updated:Sep 08, 2025

This topic describes the region switching feature of the HarmonyOS SDK.

Switch regions

The region switching feature lets you select an appropriate HTTPDNS service node based on the user's location. The SDK then uses the selected region to choose a service node that processes service scheduling and domain name resolution requests.

You can set the appropriate region in your application based on the user's geographic location.

The SDK provides two ways to set the region. The first way is during initialization:

import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { httpdns, Region } from '@aliyun/httpdns';

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

export default class EntryAbility extends UIAbility {
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
    // Configure HTTPDNS
    httpdns.configService(ACCOUNT_ID, {
      context: this.context,
      // ************* Initialize region: begin *************
      region: Region.SG,
      // ************* Initialize region: end *************
    })
  }

  // Other code is omitted
 }

The second way is to switch it dynamically:

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

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

httpdns.getService(ACCOUNT_ID).then((service) => {
  // ************* Dynamically switch region: begin *************
  service.changeRegion(Region.HK)
  // ************* Dynamically switch region: end *************
}).catch((e: HttpDnsError) => {
  console.error(`Failed ${e.code} ${e.message}`);
});

When you design your application, you should store the configured region. This ensures that the region set for HTTPDNS during initialization is the same as the one used previously.

The region has five possible values:

  • Region.MAINLAND: The Chinese mainland.

  • Region.HK: China (Hong Kong). Select this region for Southeast Asia.

  • Region.SG: Singapore. Select this region for all areas except the Chinese mainland, Europe, the Americas, or Southeast Asia.

  • Region.DE: Germany. Select this region for Europe.

  • Region.US: The United States. Select this region for the Americas.