@ohos.net.connection (Network Connection Management) (System API)

The network connection management module provides basic network management capabilities. You can obtain the default active data network or the list of all active data networks, enable or disable the airplane mode, and obtain network capability information.

NOTE 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. This topic describes only system APIs provided by the module. For details about its public APIs, see @ohos.net.connection (Network Connection Management).

Modules to Import

import connection from '@ohos.net.connection';

connection.getGlobalHttpProxy10+

getGlobalHttpProxy(callback: AsyncCallback<HttpProxy>): void

Obtains the global HTTP proxy configuration of the network. This API uses an asynchronous callback to return the result.

System API: This is a system API.

System capability: SystemCapability.Communication.NetManager.Core

Parameters

Name Type Mandatory Description
callback AsyncCallback<HttpProxy> Yes Callback used to return the result. If the global HTTP proxy configuration of the network is obtained successfully, error is undefined and data is the global HTTP proxy configuration. Otherwise, error is an error object.

Error codes

ID Error Message
401 Parameter error.
202 Non-system applications use system APIs.
2100002 Operation failed. Cannot connect to service.
2100003 System internal error.

Example

import connection from '@ohos.net.connection';
import { BusinessError } from '@ohos.base';

connection.getGlobalHttpProxy((error: BusinessError, data: connection.HttpProxy) => {
  console.info(JSON.stringify(error));
  console.info(JSON.stringify(data));
});

connection.getGlobalHttpProxy10+

getGlobalHttpProxy(): Promise<HttpProxy>;

Obtains the global HTTP proxy configuration of the network. This API uses a promise to return the result.

System API: This is a system API.

System capability: SystemCapability.Communication.NetManager.Core

Return value

Type Description
Promise<HttpProxy> Promise used to return the result.

Error codes

ID Error Message
401 Parameter error.
202 Non-system applications use system APIs.
2100002 Operation failed. Cannot connect to service.
2100003 System internal error.

Example

import connection from '@ohos.net.connection';
import { BusinessError } from '@ohos.base';

connection.getGlobalHttpProxy().then((data: connection.HttpProxy) => {
  console.info(JSON.stringify(data));
}).catch((error: BusinessError) => {
  console.info(JSON.stringify(error));
});

connection.setGlobalHttpProxy10+

setGlobalHttpProxy(httpProxy: HttpProxy, callback: AsyncCallback<void>): void

Sets the global HTTP proxy configuration of the network. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.Core

Parameters

Name Type Mandatory Description
httpProxy HttpProxy Yes Global HTTP proxy configuration of the network.
callback AsyncCallback<void> Yes Callback used to return the result. If the global HTTP proxy configuration of the network is set successfully, error is undefined. Otherwise, error is an error object.

Error codes

ID Error Message
201 Permission denied.
401 Parameter error.
202 Non-system applications use system APIs.
2100001 Invalid parameter value.
2100002 Operation failed. Cannot connect to service.
2100003 System internal error.

Example

import connection from '@ohos.net.connection';
import { BusinessError } from '@ohos.base';

let exclusionStr = "192.168,baidu.com";
let exclusionArray = exclusionStr.split(',');
connection.setGlobalHttpProxy({
  host: "192.168.xx.xxx",
  port: 8080,
  exclusionList: exclusionArray
} as connection.HttpProxy).then(() => {
  console.info("success");
}).catch((error: BusinessError) => {
  console.info(JSON.stringify(error));
});

connection.setGlobalHttpProxy10+

setGlobalHttpProxy(httpProxy: HttpProxy): Promise<void>;

Sets the global HTTP proxy configuration of the network. This API uses a promise to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.Core

Parameters

Name Type Mandatory Description
httpProxy HttpProxy Yes Global HTTP proxy configuration of the network.

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

ID Error Message
201 Permission denied.
401 Parameter error.
202 Non-system applications use system APIs.
2100001 Invalid parameter value.
2100002 Operation failed. Cannot connect to service.
2100003 System internal error.

Example

import connection from '@ohos.net.connection';
import { BusinessError } from '@ohos.base';

let exclusionStr = "192.168,baidu.com";
let exclusionArray = exclusionStr.split(',');
connection.setGlobalHttpProxy({
  host: "192.168.xx.xxx",
  port: 8080,
  exclusionList: exclusionArray
} as connection.HttpProxy).then(() => {
  console.info("success");
}).catch((error: BusinessError) => {
  console.info(JSON.stringify(error));
});

connection.enableAirplaneMode

enableAirplaneMode(callback: AsyncCallback<void>): void

Enables the airplane mode. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.Core

Parameters

Name Type Mandatory Description
callback AsyncCallback<void> Yes Callback used to return the result.

Error codes

ID Error Message
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.
2100002 Operation failed. Cannot connect to service.
2100003 System internal error.

Example

import connection from '@ohos.net.connection';
import { BusinessError } from '@ohos.base';

connection.enableAirplaneMode((error: BusinessError) => {
  console.log(JSON.stringify(error));
});

connection.enableAirplaneMode

enableAirplaneMode(): Promise<void>

Enables the airplane mode. This API uses a promise to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.Core

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

ID Error Message
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.
2100002 Operation failed. Cannot connect to service.
2100003 System internal error.

Example

import connection from '@ohos.net.connection';

connection.enableAirplaneMode().then((error: void) => {
  console.log(JSON.stringify(error));
});

connection.disableAirplaneMode

disableAirplaneMode(callback: AsyncCallback<void>): void

Disables the airplane mode. This API uses an asynchronous callback to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.Core

Parameters

Name Type Mandatory Description
callback AsyncCallback<void> Yes Callback used to return the result. If the airplane mode is disabled successfully, error is undefined. Otherwise, error is an error object.

Error codes

ID Error Message
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.
2100002 Operation failed. Cannot connect to service.
2100003 System internal error.

Example

import connection from '@ohos.net.connection';
import { BusinessError } from '@ohos.base';

connection.disableAirplaneMode((error: BusinessError) => {
  console.log(JSON.stringify(error));
});

connection.disableAirplaneMode

disableAirplaneMode(): Promise<void>

Disables the airplane mode. This API uses a promise to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.Core

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

ID Error Message
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.
2100002 Operation failed. Cannot connect to service.
2100003 System internal error.

Example

import connection from '@ohos.net.connection';

connection.disableAirplaneMode().then((error: void) => {
  console.log(JSON.stringify(error));
});

connection.factoryReset11+

factoryReset(): Promise<void>

Resets the network settings to factory defaults. This API uses a promise to return the result.

System API: This is a system API.

Required permissions: ohos.permission.CONNECTIVITY_INTERNAL

System capability: SystemCapability.Communication.NetManager.Core

Return value

Type Description
Promise<void> Promise that returns no value.

Error codes

ID Error Message
201 Permission denied.
202 Non-system applications use system APIs.
401 Parameter error.
2100002 Operation failed. Cannot connect to service.
2100003 System internal error.

Example

import connection from '@ohos.net.connection';
import { BusinessError } from '@ohos.base';
connection.factoryReset().then(() => {
    console.log("success");
}).catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
})