All Products
Search
Document Center

HTTPDNS:Integrate the HarmonyOS SDK

Last Updated:Dec 04, 2025

This topic describes how to integrate the HarmonyOS software development kit (SDK).

Introduction

This SDK is based on HarmonyOS API 12 and has a compileSdkVersion of 5.0.0(12).

Note

In HarmonyOS versions earlier than Beta5, if you use custom domain name resolution (DNS) rules to access a server using a specific IP address, you must specify the port in the URL. Otherwise, the custom DNS rules may not take effect, and the system automatically falls back to LocalDNS.

Prerequisites

  1. Understand the product workflow and obtain an Account ID.

  2. Prepare the HarmonyOS application developer environment. For more information, see the HarmonyOS Application Development Guide.

Step 1: Install the SDK

You can run the following command in the root directory of your HarmonyOS application to install the SDK:

ohpm install @aliyun/httpdns

For more information about the ohpm tool and how to install third-party SDKs for OpenHarmony, see the OpenHarmony Third-party Library Repository Guide.

Step 2: Configure the SDK

You can run the following code in the onCreate lifecycle callback of your Ability to configure the SDK:

import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { httpdns } 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 {
    // ************* Start of initialization configuration *************
    httpdns.configService(ACCOUNT_ID, {
      context: this.context,
      // Set the startup service node. You must explicitly select a target region. Valid values are Region.SG, Region.HK, Region.US, and Region.DE.
      region: Region.SG, 
      useHttps: true,
    });
    // ************* End of initialization configuration *************
  }

  // Other code is omitted
}

The ACCOUNT_ID variable is the Account ID that you obtained in the product workflow.

Important
  • If you set the useHttps parameter to true, additional billing costs are incurred. For more information, see the Billing document.

  • If your domain name information or SDNS parameters require a high level of security, you can set the aesSecretKey parameter to enable content layer encryption for resolution requests. Using content encryption incurs additional billing costs. For more information, see the Billing document.

Step 3: Use the SDK

Use the addCustomDnsRule method

Before you send a network request, you can call the SDK's domain name resolution API to perform HTTPDNS resolution. Then, use the connection.addCustomDnsRule API to configure the HTTPDNS resolution result and add a custom mapping between a host and its corresponding IP addresses for the current application. The following code shows an example of an HTTP request:

import { http } from '@kit.NetworkKit';
import connection from '@ohos.net.connection';
import { httpdns, IpType, } from '@aliyun/httpdns';
import Url from '@ohos.url';

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

export async function requestWithHttpDns(url: string, options: http.HttpRequestOptions): Promise<http.HttpResponse> {
  let urlObject = Url.URL.parseURL(url);
  const host = urlObject.hostname;
  // ************* Start of HTTPDNS resolution to get domain name *************
  const httpdnsService = await httpdns.getService(ACCOUNT_ID);
  const result = await httpdnsService.getHttpDnsResultAsync(host, IpType.Auto);
  // ************* End of HTTPDNS resolution to get domain name *************
  // ************* Start of setting DNS rules using system APIs *************
  try {
    await connection.removeCustomDnsRule(host);
  } catch (ignored) {
  }
  
  const allIps: string[] = [];
  if (result.ipv4s && result.ipv4s.length > 0) {
    allIps.push(...result.ipv4s);
  }
  if (result.ipv6s && result.ipv6s.length > 0) {
    allIps.push(...result.ipv6s);
  }
  if (allIps.length > 0) {
    console.info(`request with ${allIps?.join(',')}`);
    await connection.addCustomDnsRule(host, allIps);
  } else {
    console.log(`HTTPDNS resolution returned no results. DNS rules are not set.`);
  }
  // ************* End of setting DNS rules using system APIs *************
  // ************* Start of sending network request using system APIs *************
  const httpRequest = http.createHttp();
  return httpRequest.request(url, options);
  // ************* End of sending network request using system APIs *************
}

The ACCOUNT_ID variable is the Account ID that you obtained in the product workflow.

Important

How to customize DNS resolution rules in HarmonyOS

HarmonyOS provides the following APIs to customize DNS resolution rules: addCustomDnsRule, removeCustomDnsRule, and clearCustomDnsRules.

The addCustomDnsRule API allows an application to add a custom mapping between a host and its corresponding IP addresses.

The removeCustomDnsRule API allows an application to delete the custom DNS rule for a specific host.

The clearCustomDnsRules API allows an application to delete all custom DNS rules.

Therefore, if an application needs to customize DNS rules for network requests, you can use these APIs to set the rules before sending the network requests.

Use the dnsRules method of the Remote Communication Kit

When you use the Remote Communication Kit package to send network requests, you can first call the SDK's domain name resolution API to perform HTTPDNS resolution. Then, you can configure the dnsRules field to modify the DNS rules. The following code shows an example of a fetch request:

import { httpdns, IpType } from '@aliyun/httpdns';
import { rcp } from '@kit.RemoteCommunicationKit';
import Url from '@ohos.url';

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

export async function rcpWithHttpDns(url: string): Promise<rcp.Response> {
  let urlObject = Url.URL.parseURL(url);
  const host = urlObject.hostname;

  // ************* Start of HTTPDNS resolution to get IP results *************
  const httpdnsService = await httpdns.getService(ACCOUNT_ID);
  const httpdnsResult = await httpdnsService.getHttpDnsResultAsync(host, IpType.Auto);
  let addresses: string[] = [];
  if (httpdnsResult.ipv4s) {
    addresses.push(...httpdnsResult.ipv4s)
  }
  if (httpdnsResult.ipv6s) {
    addresses.push(...httpdnsResult.ipv6s)
  }
  // ************* End of HTTPDNS resolution to get IP results *************

  const request = new rcp.Request(url, "GET");
  request.configuration = {
    dns: {
      // ************* Start of setting IP addresses using dnsRules *************
      dnsRules: [{
        host,
        port: 80,
        ipAddresses: addresses
      }, {
        host,
        port: 443,
        ipAddresses: addresses
      }]
      // ************* End of setting IP addresses using dnsRules *************
    }
  }

  // ************* Start of sending network request using system APIs *************
  const session = rcp.createSession();
  return session.fetch(request);
  // ************* End of sending network request using system APIs *************
}

The ACCOUNT_ID variable is the Account ID that you obtained in the product workflow.

What to do next

To configure the SDK with more specific settings or use other features, such as authenticated requests, see the HarmonyOS SDK API.