1e41f4b71Sopenharmony_ci# Network Connection Management
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Introduction
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciThe Network Connection Management module provides basic network management capabilities, including management of Wi-Fi/cellular/Ethernet connection priorities, network quality evaluation, subscription to network connection status changes, query of network connection information, and DNS resolution.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci> **NOTE**
8e41f4b71Sopenharmony_ci> To maximize the application running efficiency, most API calls are called asynchronously in callback or promise mode. The following code examples use the promise mode. For details about the APIs, see [API Reference](../reference/apis-network-kit/js-apis-net-connection.md).
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci## Basic Concepts
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ci- Producer: a provider of data networks, such as Wi-Fi, cellular, and Ethernet.
13e41f4b71Sopenharmony_ci- Consumer: a user of data networks, for example, an application or a system service.
14e41f4b71Sopenharmony_ci- Network probe: a mechanism used to detect the network availability to prevent the switch from an available network to an unavailable network. The probe type can be binding network detection, DNS detection, HTTP detection, or HTTPS detection.
15e41f4b71Sopenharmony_ci- Network selection: a mechanism used to select the optimal network when multiple networks coexist. It is triggered when the network status, network information, or network quality evaluation score changes.
16e41f4b71Sopenharmony_ci
17e41f4b71Sopenharmony_ci## **Constraints**
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci- Programming language: JS
20e41f4b71Sopenharmony_ci- The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci## When to Use
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ciTypical application scenarios of network connection management are as follows:
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci- Subscribing to status changes of the specified network
27e41f4b71Sopenharmony_ci- Obtaining the list of all registered networks
28e41f4b71Sopenharmony_ci- Querying network connection information based on the data network
29e41f4b71Sopenharmony_ci- Resolving the domain name of a network to obtain all IP addresses
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ciThe following describes the development procedure specific to each application scenario.
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci## Available APIs
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ciFor the complete list of APIs and example code, see [Network Connection Management](../reference/apis-network-kit/js-apis-net-connection.md).
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci| API| Description|
38e41f4b71Sopenharmony_ci| ---- | ---- |
39e41f4b71Sopenharmony_ci| getDefaultNet(callback: AsyncCallback\<NetHandle>): void; |Creates a **NetHandle** object that contains the **netId** of the default network. This API uses an asynchronous callback to return the result.|
40e41f4b71Sopenharmony_ci| getGlobalHttpProxy(callback: AsyncCallback\<HttpProxy>): void;| Obtains the global HTTP proxy for the network. This API uses an asynchronous callback to return the result.|
41e41f4b71Sopenharmony_ci| setGlobalHttpProxy(httpProxy: HttpProxy, callback: AsyncCallback\<void>): void;| Sets the global HTTP proxy for the network. This API uses an asynchronous callback to return the result.|
42e41f4b71Sopenharmony_ci| setAppHttpProxy(httpProxy: HttpProxy): void;| Sets the application-level HTTP proxy configuration of the network.|
43e41f4b71Sopenharmony_ci| getAppNet(callback: AsyncCallback\<NetHandle>): void;| Obtains a **NetHandle** object that contains the **netId** of the network bound to the application. This API uses an asynchronous callback to return the result.|
44e41f4b71Sopenharmony_ci| setAppNet(netHandle: NetHandle, callback: AsyncCallback\<void>): void;| Binds an application to the specified network. The application can access the external network only through this network. This API uses an asynchronous callback to return the result.|
45e41f4b71Sopenharmony_ci| getDefaultNetSync(): NetHandle; |Obtains the default active data network in synchronous mode. You can use **getNetCapabilities** to obtain information such as the network type and capabilities.|
46e41f4b71Sopenharmony_ci| hasDefaultNet(callback: AsyncCallback\<boolean>): void; |Checks whether the default data network is activated. This API uses an asynchronous callback to return the result.|
47e41f4b71Sopenharmony_ci| getAllNets(callback: AsyncCallback\<Array\<NetHandle>>): void;| Obtains the list of **NetHandle** objects of the connected network. This API uses an asynchronous callback to return the result.|
48e41f4b71Sopenharmony_ci| getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback\<ConnectionProperties>): void; |Obtains network connection information of the network corresponding to the **netHandle**. This API uses an asynchronous callback to return the result.|
49e41f4b71Sopenharmony_ci| getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback\<NetCapabilities>): void; |Obtains capability information of the network corresponding to the **netHandle**. This API uses an asynchronous callback to return the result.|
50e41f4b71Sopenharmony_ci| isDefaultNetMetered(callback: AsyncCallback\<boolean>): void; |Checks whether the data traffic usage on the current network is metered. This API uses an asynchronous callback to return the result.|
51e41f4b71Sopenharmony_ci| reportNetConnected(netHandle: NetHandle, callback: AsyncCallback\<void>): void;| Reports a **netAavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager. This API uses an asynchronous callback to return the result.|
52e41f4b71Sopenharmony_ci| reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback\<void>): void;| Reports a **netAavailable** event to NetManager. If this API is called, the application considers that its network status (ohos.net.connection.NetCap.NET_CAPABILITY_VAILDATED) is inconsistent with that of NetManager. This API uses an asynchronous callback to return the result.|
53e41f4b71Sopenharmony_ci| getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void; |Obtains all IP addresses of the specified network by resolving the domain name. This API uses an asynchronous callback to return the result.|
54e41f4b71Sopenharmony_ci| enableAirplaneMode(callback: AsyncCallback\<void>): void; | Enables the airplane mode. This API uses an asynchronous callback to return the result.|
55e41f4b71Sopenharmony_ci| disableAirplaneMode(callback: AsyncCallback\<void>): void;| Disables the airplane mode. This API uses an asynchronous callback to return the result.|
56e41f4b71Sopenharmony_ci| createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection; | Creates a **NetConnection** object. **netSpecifier** specifies the network, and **timeout** specifies the timeout interval in ms. **timeout** is configurable only when **netSpecifier** is specified. If neither of them is present, the default network is used.|
57e41f4b71Sopenharmony_ci| bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void; | Binds a **TCPSocket** or **UDPSocket** to the current network. This API uses an asynchronous callback to return the result.|
58e41f4b71Sopenharmony_ci| getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void; |Obtains all IP addresses of the specified network by resolving the domain name. This API uses an asynchronous callback to return the result.|
59e41f4b71Sopenharmony_ci| getAddressByName(host: string, callback: AsyncCallback\<NetAddress>): void; |Obtains an IP address of the specified network by resolving the domain name. This API uses an asynchronous callback to return the result.|
60e41f4b71Sopenharmony_ci| on(type: 'netAvailable', callback: Callback\<NetHandle>): void;                   |Subscribes to **netAvailable** events.|
61e41f4b71Sopenharmony_ci| on(type: 'netCapabilitiesChange', callback: Callback\<NetCapabilityInfo\>): void; |Subscribes to **netCapabilitiesChange** events.|
62e41f4b71Sopenharmony_ci| on(type: 'netConnectionPropertiesChange', callback: Callback\<{ netHandle: NetHandle, connectionProperties: ConnectionProperties }>): void; |Subscribes to **netConnectionPropertiesChange** events.|
63e41f4b71Sopenharmony_ci| on(type: 'netBlockStatusChange', callback: Callback<{ netHandle: NetHandle, blocked: boolean }>): void; |Subscribes to **netBlockStatusChange** events.|
64e41f4b71Sopenharmony_ci| on(type: 'netLost', callback: Callback\<NetHandle>): void; |Subscribes to **netLost** events.|
65e41f4b71Sopenharmony_ci| on(type: 'netUnavailable', callback: Callback\<void>): void; |Subscribes to **netUnavailable** events.|
66e41f4b71Sopenharmony_ci| register(callback: AsyncCallback\<void>): void; |Subscribes to network status changes.|
67e41f4b71Sopenharmony_ci| unregister(callback: AsyncCallback\<void>): void; |Unsubscribes from network status changes.|
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci## Subscribing to Status Changes of the Specified Network
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci1. Declare the required permission: **ohos.permission.GET_NETWORK_INFO**.
72e41f4b71Sopenharmony_ciThis permission is of the **normal** level. Before applying for the permission, ensure that the [basic principles for permission management](../security/AccessToken/app-permission-mgmt-overview.md#basic-principles-for-using-permissions) are met. Declare the permissions required by your application. For details, see [Declaring Permissions in the Configuration File](accesstoken-guidelines.md#declaring-permissions-in-the configuration-file).
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci1. Import the **connection** namespace from **@kit.NetworkKit**.
75e41f4b71Sopenharmony_ci
76e41f4b71Sopenharmony_ci2. Call **createNetConnection()** to create a **NetConnection** object. You can specify the network type, capability, and timeout interval. If you do not specify parameters, the default values will be used. 
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci3. Call **conn.on()** to subscribe to the target event. You must pass in **type** and **callback**.
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci4. Call **conn.register()** to subscribe to network status changes of the specified network.
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci5. When the network is available, the callback will be invoked to return the **netAvailable** event. When the network is unavailable, the callback will be invoked to return the **netUnavailable** event.
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci6. Call **conn.unregister()** to unsubscribe from the network status changes if required.
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci```ts
87e41f4b71Sopenharmony_ci// Import the connection namespace.
88e41f4b71Sopenharmony_ciimport { connection } from '@kit.NetworkKit';
89e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_cilet netSpecifier: connection.NetSpecifier = {
92e41f4b71Sopenharmony_ci  netCapabilities: {
93e41f4b71Sopenharmony_ci    // Assume that the default network is Wi-Fi. If you need to create a cellular network connection, set the network type to CELLULAR.
94e41f4b71Sopenharmony_ci    bearerTypes: [connection.NetBearType.BEARER_CELLULAR],
95e41f4b71Sopenharmony_ci    // Set the network capability to INTERNET.
96e41f4b71Sopenharmony_ci    networkCap: [connection.NetCap.NET_CAPABILITY_INTERNET]
97e41f4b71Sopenharmony_ci  },
98e41f4b71Sopenharmony_ci};
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci// Set the timeout value to 10s. The default value is 0.
101e41f4b71Sopenharmony_cilet timeout = 10 * 1000;
102e41f4b71Sopenharmony_ci
103e41f4b71Sopenharmony_ci// Create a NetConnection object.
104e41f4b71Sopenharmony_cilet conn = connection.createNetConnection(netSpecifier, timeout);
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_ci// Register an observer for network status changes.
107e41f4b71Sopenharmony_ciconn.register((err: BusinessError, data: void) => {
108e41f4b71Sopenharmony_ci  console.log(JSON.stringify(err));
109e41f4b71Sopenharmony_ci});
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ci// Listen to network status change events. If the network is available, an on_netAvailable event is returned.
112e41f4b71Sopenharmony_ciconn.on('netAvailable', ((data: connection.NetHandle) => {
113e41f4b71Sopenharmony_ci  console.log("net is available, netId is " + data.netId);
114e41f4b71Sopenharmony_ci}));
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci// Listen to network status change events. If the network is unavailable, an on_netUnavailable event is returned.
117e41f4b71Sopenharmony_ciconn.on('netUnavailable', ((data: void) => {
118e41f4b71Sopenharmony_ci  console.log("net is unavailable, data is " + JSON.stringify(data));
119e41f4b71Sopenharmony_ci}));
120e41f4b71Sopenharmony_ci
121e41f4b71Sopenharmony_ci// Unregister the observer for network status changes.
122e41f4b71Sopenharmony_ciconn.unregister((err: BusinessError, data: void) => {
123e41f4b71Sopenharmony_ci});
124e41f4b71Sopenharmony_ci```
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci## Obtaining the List of All Registered Networks
127e41f4b71Sopenharmony_ci
128e41f4b71Sopenharmony_ci1. Declare the required permission: **ohos.permission.GET_NETWORK_INFO**.
129e41f4b71Sopenharmony_ciThis permission is of the **normal** level. Before applying for the permission, ensure that the [basic principles for permission management](../security/AccessToken/app-permission-mgmt-overview.md#basic-principles-for-using-permissions) are met. Declare the permissions required by your application. For details, see [Declaring Permissions in the Configuration File](accesstoken-guidelines.md#declaring-permissions-in-the configuration-file).
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ci2. Import the **connection** namespace from **@kit.NetworkKit**.
132e41f4b71Sopenharmony_ci
133e41f4b71Sopenharmony_ci3. Call **getAllNets** to obtain the list of all connected networks.
134e41f4b71Sopenharmony_ci
135e41f4b71Sopenharmony_ci```ts
136e41f4b71Sopenharmony_ci// Import the connection namespace.
137e41f4b71Sopenharmony_ciimport { connection } from '@kit.NetworkKit';
138e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
139e41f4b71Sopenharmony_ci
140e41f4b71Sopenharmony_ci// Construct a singleton object.
141e41f4b71Sopenharmony_ciexport class GlobalContext {
142e41f4b71Sopenharmony_ci  public netList: connection.NetHandle[] = [];
143e41f4b71Sopenharmony_ci  private constructor() {}
144e41f4b71Sopenharmony_ci  private static instance: GlobalContext;
145e41f4b71Sopenharmony_ci  private _objects = new Map<string, Object>();
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci  public static getContext(): GlobalContext {
148e41f4b71Sopenharmony_ci    if (!GlobalContext.instance) {
149e41f4b71Sopenharmony_ci      GlobalContext.instance = new GlobalContext();
150e41f4b71Sopenharmony_ci    }
151e41f4b71Sopenharmony_ci    return GlobalContext.instance;
152e41f4b71Sopenharmony_ci  }
153e41f4b71Sopenharmony_ci
154e41f4b71Sopenharmony_ci  getObject(value: string): Object | undefined {
155e41f4b71Sopenharmony_ci    return this._objects.get(value);
156e41f4b71Sopenharmony_ci  }
157e41f4b71Sopenharmony_ci
158e41f4b71Sopenharmony_ci  setObject(key: string, objectClass: Object): void {
159e41f4b71Sopenharmony_ci    this._objects.set(key, objectClass);
160e41f4b71Sopenharmony_ci  }
161e41f4b71Sopenharmony_ci}
162e41f4b71Sopenharmony_ci
163e41f4b71Sopenharmony_ci// Obtain the list of all connected networks.
164e41f4b71Sopenharmony_ciconnection.getAllNets().then((data: connection.NetHandle[]) => {
165e41f4b71Sopenharmony_ci  console.info("Succeeded to get data: " + JSON.stringify(data));
166e41f4b71Sopenharmony_ci  if (data) {
167e41f4b71Sopenharmony_ci    GlobalContext.getContext().netList = data;
168e41f4b71Sopenharmony_ci  }
169e41f4b71Sopenharmony_ci});
170e41f4b71Sopenharmony_ci```
171e41f4b71Sopenharmony_ci
172e41f4b71Sopenharmony_ci## Querying Network Capability Information and Connection Information of Specified Data Network
173e41f4b71Sopenharmony_ci
174e41f4b71Sopenharmony_ci1. Declare the required permission: **ohos.permission.GET_NETWORK_INFO**.
175e41f4b71Sopenharmony_ciThis permission is of the **normal** level. Before applying for the permission, ensure that the [basic principles for permission management](../security/AccessToken/app-permission-mgmt-overview.md#basic-principles-for-using-permissions) are met. Declare the permissions required by your application. For details, see [Declaring Permissions in the Configuration File](accesstoken-guidelines.md#declaring-permissions-in-the configuration-file).
176e41f4b71Sopenharmony_ci
177e41f4b71Sopenharmony_ci2. Import the **connection** namespace from **@kit.NetworkKit**.
178e41f4b71Sopenharmony_ci
179e41f4b71Sopenharmony_ci3. Call **getDefaultNet** to obtain the default data network via **NetHandle** or call **getAllNets** to obtain the list of all connected networks via **Array\<NetHandle>**.
180e41f4b71Sopenharmony_ci
181e41f4b71Sopenharmony_ci4. Call **getNetCapabilities** to obtain the network capability information of the data network specified by **NetHandle**. The capability information includes information such as the network type (cellular, Wi-Fi, or Ethernet network) and the specific network capabilities.
182e41f4b71Sopenharmony_ci
183e41f4b71Sopenharmony_ci5. Call **getConnectionProperties** to obtain the connection information of the data network specified by **NetHandle**.
184e41f4b71Sopenharmony_ci
185e41f4b71Sopenharmony_ci```ts
186e41f4b71Sopenharmony_ciimport { connection } from '@kit.NetworkKit';
187e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci// Construct a singleton object.
190e41f4b71Sopenharmony_ciexport class GlobalContext {
191e41f4b71Sopenharmony_ci  public netList: connection.NetHandle[] = [];
192e41f4b71Sopenharmony_ci  public netHandle: connection.NetHandle|null = null;
193e41f4b71Sopenharmony_ci  private constructor() {}
194e41f4b71Sopenharmony_ci  private static instance: GlobalContext;
195e41f4b71Sopenharmony_ci  private _objects = new Map<string, Object>();
196e41f4b71Sopenharmony_ci
197e41f4b71Sopenharmony_ci  public static getContext(): GlobalContext {
198e41f4b71Sopenharmony_ci    if (!GlobalContext.instance) {
199e41f4b71Sopenharmony_ci      GlobalContext.instance = new GlobalContext();
200e41f4b71Sopenharmony_ci    }
201e41f4b71Sopenharmony_ci    return GlobalContext.instance;
202e41f4b71Sopenharmony_ci  }
203e41f4b71Sopenharmony_ci
204e41f4b71Sopenharmony_ci  getObject(value: string): Object | undefined {
205e41f4b71Sopenharmony_ci    return this._objects.get(value);
206e41f4b71Sopenharmony_ci  }
207e41f4b71Sopenharmony_ci
208e41f4b71Sopenharmony_ci  setObject(key: string, objectClass: Object): void {
209e41f4b71Sopenharmony_ci    this._objects.set(key, objectClass);
210e41f4b71Sopenharmony_ci  }
211e41f4b71Sopenharmony_ci}
212e41f4b71Sopenharmony_ci
213e41f4b71Sopenharmony_ci// Call getDefaultNet to obtain the default data network specified by **NetHandle**.
214e41f4b71Sopenharmony_ciconnection.getDefaultNet().then((data:connection.NetHandle) => {
215e41f4b71Sopenharmony_ci  if (data.netId == 0) {
216e41f4b71Sopenharmony_ci    // If the obtained netid of netHandler is 0 when no default network is specified, an exception has occurred and extra processing is needed.
217e41f4b71Sopenharmony_ci    return;
218e41f4b71Sopenharmony_ci  }
219e41f4b71Sopenharmony_ci  if (data) {
220e41f4b71Sopenharmony_ci    console.info("getDefaultNet get data: " + JSON.stringify(data));
221e41f4b71Sopenharmony_ci    GlobalContext.getContext().netHandle = data;
222e41f4b71Sopenharmony_ci    // Obtain the network capability information of the data network specified by **NetHandle**. The capability information includes information such as the network type and specific network capabilities.
223e41f4b71Sopenharmony_ci    connection.getNetCapabilities(GlobalContext.getContext().netHandle).then(
224e41f4b71Sopenharmony_ci      (data: connection.NetCapabilities) => {
225e41f4b71Sopenharmony_ci      console.info("getNetCapabilities get data: " + JSON.stringify(data));
226e41f4b71Sopenharmony_ci      // Obtain the network type via bearerTypes.
227e41f4b71Sopenharmony_ci      let bearerTypes: Set<number> = new Set(data.bearerTypes);
228e41f4b71Sopenharmony_ci      let bearerTypesNum = Array.from(bearerTypes.values());
229e41f4b71Sopenharmony_ci      for (let item of bearerTypesNum) {
230e41f4b71Sopenharmony_ci        if (item == 0) {
231e41f4b71Sopenharmony_ci          // Cellular network
232e41f4b71Sopenharmony_ci          console.log(JSON.stringify("BEARER_CELLULAR"));
233e41f4b71Sopenharmony_ci        } else if (item == 1) {
234e41f4b71Sopenharmony_ci          // Wi-Fi network
235e41f4b71Sopenharmony_ci          console.log(JSON.stringify("BEARER_WIFI"));
236e41f4b71Sopenharmony_ci        } else if (item == 3) {
237e41f4b71Sopenharmony_ci          // Ethernet network
238e41f4b71Sopenharmony_ci          console.log(JSON.stringify("BEARER_ETHERNET"));
239e41f4b71Sopenharmony_ci        }
240e41f4b71Sopenharmony_ci      }
241e41f4b71Sopenharmony_ci
242e41f4b71Sopenharmony_ci      // Obtain the specific network capabilities via networkCap.
243e41f4b71Sopenharmony_ci      let itemNumber : Set<number> = new Set(data.networkCap);
244e41f4b71Sopenharmony_ci      let dataNumber = Array.from(itemNumber.values());
245e41f4b71Sopenharmony_ci      for (let item of dataNumber) {
246e41f4b71Sopenharmony_ci        if (item == 0) {
247e41f4b71Sopenharmony_ci          // The network can connect to the carrier's Multimedia Messaging Service Center (MMSC) to send and receive multimedia messages.
248e41f4b71Sopenharmony_ci          console.log(JSON.stringify("NET_CAPABILITY_MMS"));
249e41f4b71Sopenharmony_ci        } else if (item == 11) {
250e41f4b71Sopenharmony_ci          // The network traffic is not metered.
251e41f4b71Sopenharmony_ci          console.log(JSON.stringify("NET_CAPABILITY_NOT_METERED"));
252e41f4b71Sopenharmony_ci        } else if (item == 12) {
253e41f4b71Sopenharmony_ci          // The network has the Internet access capability, which is set by the network provider.
254e41f4b71Sopenharmony_ci          console.log(JSON.stringify("NET_CAPABILITY_INTERNET"));
255e41f4b71Sopenharmony_ci        } else if (item == 15) {
256e41f4b71Sopenharmony_ci          // The network does not use a Virtual Private Network (VPN).
257e41f4b71Sopenharmony_ci          console.log(JSON.stringify("NET_CAPABILITY_NOT_VPN"));
258e41f4b71Sopenharmony_ci        } else if (item == 16) {
259e41f4b71Sopenharmony_ci          // The Internet access capability of the network is successfully verified by the connection management module.
260e41f4b71Sopenharmony_ci          console.log(JSON.stringify("NET_CAPABILITY_VALIDATED"));
261e41f4b71Sopenharmony_ci        }
262e41f4b71Sopenharmony_ci      }
263e41f4b71Sopenharmony_ci    })
264e41f4b71Sopenharmony_ci  }
265e41f4b71Sopenharmony_ci});
266e41f4b71Sopenharmony_ci
267e41f4b71Sopenharmony_ci// Obtain the connection information of the data network specified by NetHandle. Connection information includes link and route information.
268e41f4b71Sopenharmony_ciconnection.getConnectionProperties(GlobalContext.getContext().netHandle).then((data: connection.ConnectionProperties) => {
269e41f4b71Sopenharmony_ci  console.info("getConnectionProperties get data: " + JSON.stringify(data));
270e41f4b71Sopenharmony_ci})
271e41f4b71Sopenharmony_ci
272e41f4b71Sopenharmony_ci// Call getAllNets to obtain the list of all connected networks via Array<NetHandle>.
273e41f4b71Sopenharmony_ciconnection.getAllNets().then((data: connection.NetHandle[]) => {
274e41f4b71Sopenharmony_ci  console.info("getAllNets get data: " + JSON.stringify(data));
275e41f4b71Sopenharmony_ci  if (data) {
276e41f4b71Sopenharmony_ci    GlobalContext.getContext().netList = data;
277e41f4b71Sopenharmony_ci
278e41f4b71Sopenharmony_ci    let itemNumber : Set<connection.NetHandle> = new Set(GlobalContext.getContext().netList);
279e41f4b71Sopenharmony_ci    let dataNumber = Array.from(itemNumber.values());
280e41f4b71Sopenharmony_ci    for (let item of dataNumber) {
281e41f4b71Sopenharmony_ci      // Obtain the network capability information of the network specified by each netHandle on the network list cyclically.
282e41f4b71Sopenharmony_ci      connection.getNetCapabilities(item).then((data: connection.NetCapabilities) => {
283e41f4b71Sopenharmony_ci        console.info("getNetCapabilities get data: " + JSON.stringify(data));
284e41f4b71Sopenharmony_ci      })
285e41f4b71Sopenharmony_ci
286e41f4b71Sopenharmony_ci      // Obtain the connection information of the network specified by each netHandle on the network list cyclically.
287e41f4b71Sopenharmony_ci      connection.getConnectionProperties(item).then((data: connection.ConnectionProperties) => {
288e41f4b71Sopenharmony_ci        console.info("getConnectionProperties get data: " + JSON.stringify(data));
289e41f4b71Sopenharmony_ci      })
290e41f4b71Sopenharmony_ci    }
291e41f4b71Sopenharmony_ci  }
292e41f4b71Sopenharmony_ci})
293e41f4b71Sopenharmony_ci```
294e41f4b71Sopenharmony_ci
295e41f4b71Sopenharmony_ci## Resolving the domain name of a network to obtain all IP addresses
296e41f4b71Sopenharmony_ci
297e41f4b71Sopenharmony_ci1. Declare the required permission: **ohos.permission.INTERNET**.
298e41f4b71Sopenharmony_ciThis permission is of the **normal** level. Before applying for the permission, ensure that the [basic principles for permission management](../security/AccessToken/app-permission-mgmt-overview.md#basic-principles-for-using-permissions) are met. Declare the permissions required by your application. For details, see [Declaring Permissions in the Configuration File](accesstoken-guidelines.md#declaring-permissions-in-the configuration-file).
299e41f4b71Sopenharmony_ci
300e41f4b71Sopenharmony_ci2. Import the **connection** namespace from **@kit.NetworkKit**.
301e41f4b71Sopenharmony_ci
302e41f4b71Sopenharmony_ci3. Call **getAddressesByName** to use the default network to resolve the host name to obtain the list of all IP addresses.
303e41f4b71Sopenharmony_ci
304e41f4b71Sopenharmony_ci```ts
305e41f4b71Sopenharmony_ci// Import the connection namespace.
306e41f4b71Sopenharmony_ciimport { connection } from '@kit.NetworkKit';
307e41f4b71Sopenharmony_ciimport { BusinessError } from '@kit.BasicServicesKit';
308e41f4b71Sopenharmony_ci
309e41f4b71Sopenharmony_ci// Use the default network to resolve the host name to obtain the list of all IP addresses.
310e41f4b71Sopenharmony_ciconnection.getAddressesByName("xxxx").then((data: connection.NetAddress[]) => {
311e41f4b71Sopenharmony_ci  console.info("Succeeded to get data: " + JSON.stringify(data));
312e41f4b71Sopenharmony_ci});
313e41f4b71Sopenharmony_ci```
314