openharmony 鸿蒙 js-apis-net-connection-sys

2025-06-12 浏览 (1)

@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 '@kit.NetworkKit';

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

NameTypeMandatoryDescription
callbackAsyncCallback<HttpProxy>YesCallback 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

IDError Message
401Parameter error.
202Non-system applications use system APIs.
2100002Failed to connect to the service.
2100003System internal error.

Example

import { connection } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

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

TypeDescription
Promise<HttpProxy>Promise used to return the result.

Error codes

IDError Message
202Non-system applications use system APIs.
2100002Failed to connect to the service.
2100003System internal error.

Example

import { connection } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

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

NameTypeMandatoryDescription
httpProxyHttpProxyYesGlobal HTTP proxy configuration of the network.
callbackAsyncCallback<void>YesCallback 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

IDError Message
201Permission denied.
401Parameter error.
202Non-system applications use system APIs.
2100001Invalid parameter value.
2100002Failed to connect to the service.
2100003System internal error.

Example

import { connection } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

let exclusionStr = "192.168,baidu.com";
let exclusionArray = exclusionStr.split(',');
let httpProxy: connection.HttpProxy = {
    host: "192.168.xx.xxx",
    port: 8080,
    exclusionList: exclusionArray
}
connection.setGlobalHttpProxy(httpProxy, (err: BusinessError) => {
    if (err) {
        console.error(`setGlobalHttpProxy failed, callback: err->${JSON.stringify(err)}`);
        return;
    }
    console.log(`setGlobalHttpProxy success.`);
});

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

NameTypeMandatoryDescription
httpProxyHttpProxyYesGlobal HTTP proxy configuration of the network.

Return value

TypeDescription
Promise<void>Promise that returns no value.

Error codes

IDError Message
201Permission denied.
401Parameter error.
202Non-system applications use system APIs.
2100001Invalid parameter value.
2100002Failed to connect to the service.
2100003System internal error.

Example

import { connection } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

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

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100002Failed to connect to the service.
2100003System internal error.

Example

import { connection } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

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

TypeDescription
Promise<void>Promise that returns no value.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
2100002Failed to connect to the service.
2100003System internal error.

Example

import { connection } from '@kit.NetworkKit';

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

NameTypeMandatoryDescription
callbackAsyncCallback<void>YesCallback used to return the result. If the airplane mode is disabled successfully, error is undefined. Otherwise, error is an error object.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
401Parameter error.
2100002Failed to connect to the service.
2100003System internal error.

Example

import { connection } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

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

TypeDescription
Promise<void>Promise that returns no value.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
2100002Failed to connect to the service.
2100003System internal error.

Example

import { connection } from '@kit.NetworkKit';

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

TypeDescription
Promise<void>Promise that returns no value.

Error codes

IDError Message
201Permission denied.
202Non-system applications use system APIs.
2100002Failed to connect to the service.
2100003System internal error.

Example

import { connection } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';

connection.factoryReset().then(() => {
    console.log("success");
}).catch((error: BusinessError) => {
    console.log(JSON.stringify(error));
})

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Network Kit

harmony 鸿蒙NetConn_ConnectionProperties

harmony 鸿蒙NetConn_HttpProxy

harmony 鸿蒙NetConn_NetAddr

harmony 鸿蒙NetConn_NetCapabilities

harmony 鸿蒙NetConn_NetConnCallback

harmony 鸿蒙NetConn_NetHandle

harmony 鸿蒙NetConn_NetHandleList

harmony 鸿蒙NetConn_NetSpecifier

harmony 鸿蒙NetConn_Route

  • 所属分类: 后端技术
  • 本文标签: 鸿蒙 软件
  • 版权声明: 本文链接 https://seaxiang.com/blog/oQjRlr