1# @ohos.enterprise.networkManager(网络管理) 2 3本模块提供设备网络管理能力,包括查询设备IP地址、MAC地址信息等。 4 5> **说明:** 6> 7> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9> 本模块接口仅可在Stage模型下使用。 10> 11> 本模块接口仅对[设备管理应用](enterpriseDeviceManagement-overview.md#基本概念)开放,需将[设备管理应用激活](js-apis-enterprise-adminManager.md#adminmanagerenableadmin)后调用,实现相应功能。 12 13## 导入模块 14 15```ts 16import networkManager from '@ohos.enterprise.networkManager'; 17``` 18 19## networkManager.getAllNetworkInterfaces 20 21getAllNetworkInterfaces(admin: Want, callback: AsyncCallback<Array<string>>): void 22 23指定设备管理应用获取所有激活的网络接口。使用callback异步回调。 24 25**需要权限:** ohos.permission.ENTERPRISE_GET_NETWORK_INFO 26 27**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 28 29**系统API**: 此接口为系统接口。 30 31**参数:** 32 33| 参数名 | 类型 | 必填 | 说明 | 34| -------- | ---------------------------------------- | ---- | ------------------------------- | 35| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 36| callback | AsyncCallback<Array<string>> | 是 | 回调函数。当接口调用成功,err为null,data为网络接口名称数组,否则err为错误对象。 | 37 38**错误码**: 39 40以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 41 42| 错误码ID | 错误信息 | 43| ------- | ---------------------------------------------------------------------------- | 44| 9200001 | the application is not an administrator of the device. | 45| 9200002 | the administrator application does not have permission to manage the device. | 46 47**示例:** 48 49```ts 50import Want from '@ohos.app.ability.Want'; 51let wantTemp: Want = { 52 bundleName: 'com.example.myapplication', 53 abilityName: 'EntryAbility', 54}; 55 56networkManager.getAllNetworkInterfaces(wantTemp, (err, result) => { 57 if (err) { 58 console.error(`Failed to get all network interfaces. Code: ${err.code}, message: ${err.message}`); 59 return; 60 } 61 console.info(`Succeeded in getting all network interfaces, result : ${JSON.stringify(result)}`); 62}); 63``` 64 65## networkManager.getAllNetworkInterfaces 66 67getAllNetworkInterfaces(admin: Want): Promise<Array<string>> 68 69指定设备管理应用获取所有激活的网络接口。使用Promise异步回调。 70 71**需要权限:** ohos.permission.ENTERPRISE_GET_NETWORK_INFO 72 73**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 74 75**系统API**: 此接口为系统接口。 76 77**参数:** 78 79| 参数名 | 类型 | 必填 | 说明 | 80| ----- | ----------------------------------- | ---- | ------- | 81| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 82 83**返回值:** 84 85| 类型 | 说明 | 86| --------------------- | ------------------------- | 87| Promise<Array<string>> | Promise结果,返回网络接口名称数组。 | 88 89**错误码**: 90 91以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 92 93| 错误码ID | 错误信息 | 94| ------- | ---------------------------------------------------------------------------- | 95| 9200001 | the application is not an administrator of the device. | 96| 9200002 | the administrator application does not have permission to manage the device. | 97 98**示例:** 99 100```ts 101import Want from '@ohos.app.ability.Want'; 102import { BusinessError } from '@ohos.base'; 103let wantTemp: Want = { 104 bundleName: 'com.example.myapplication', 105 abilityName: 'EntryAbility', 106}; 107 108networkManager.getAllNetworkInterfaces(wantTemp).then((result) => { 109 console.info(`Succeeded in getting all network interfaces, result : ${JSON.stringify(result)}`); 110}).catch((err: BusinessError) => { 111 console.error(`Failed to get all network interfaces. Code: ${err.code}, message: ${err.message}`); 112}); 113``` 114 115## networkManager.getIpAddress 116 117getIpAddress(admin: Want, networkInterface: string, callback: AsyncCallback<string>): void 118 119指定设备管理应用根据网络接口获取设备IP地址。使用callback异步回调。 120 121**需要权限:** ohos.permission.ENTERPRISE_GET_NETWORK_INFO 122 123**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 124 125**系统API**: 此接口为系统接口。 126 127**参数:** 128 129| 参数名 | 类型 | 必填 | 说明 | 130| -------- | ---------------------------------------- | ---- | ------------------------------- | 131| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 132| networkInterface | string | 是 | 指定网络接口。 | 133| callback | AsyncCallback<string> | 是 | 回调函数。当接口调用成功,err为null,data为IP地址,否则err为错误对象。 | 134 135**错误码**: 136 137以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 138 139| 错误码ID | 错误信息 | 140| ------- | ---------------------------------------------------------------------------- | 141| 9200001 | the application is not an administrator of the device. | 142| 9200002 | the administrator application does not have permission to manage the device. | 143 144**示例:** 145 146```ts 147import Want from '@ohos.app.ability.Want'; 148let wantTemp: Want = { 149 bundleName: 'com.example.myapplication', 150 abilityName: 'EntryAbility', 151}; 152 153networkManager.getIpAddress(wantTemp, 'eth0', (err, result) => { 154 if (err) { 155 console.error(`Failed to get ip address. Code: ${err.code}, message: ${err.message}`); 156 return; 157 } 158 console.info(`Succeeded in getting ip address, result : ${result}`); 159}); 160``` 161 162## networkManager.getIpAddress 163 164getIpAddress(admin: Want, networkInterface: string): Promise<string> 165 166指定设备管理应用根据网络接口获取设备IP地址。使用Promise异步回调。 167 168**需要权限:** ohos.permission.ENTERPRISE_GET_NETWORK_INFO 169 170**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 171 172**系统API**: 此接口为系统接口。 173 174**参数:** 175 176| 参数名 | 类型 | 必填 | 说明 | 177| ----- | ----------------------------------- | ---- | ------- | 178| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 179| networkInterface | string | 是 | 指定网络接口。 | 180 181**返回值:** 182 183| 类型 | 说明 | 184| --------------------- | ------------------------- | 185| Promise<string> | Promise结果,返回设备IP地址。 | 186 187**错误码**: 188 189以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 190 191| 错误码ID | 错误信息 | 192| ------- | ---------------------------------------------------------------------------- | 193| 9200001 | the application is not an administrator of the device. | 194| 9200002 | the administrator application does not have permission to manage the device. | 195 196**示例:** 197 198```ts 199import Want from '@ohos.app.ability.Want'; 200import { BusinessError } from '@ohos.base'; 201let wantTemp: Want = { 202 bundleName: 'com.example.myapplication', 203 abilityName: 'EntryAbility', 204}; 205 206networkManager.getIpAddress(wantTemp, 'eth0').then((result) => { 207 console.info(`Succeeded in getting ip address, result : ${result}`); 208}).catch((err: BusinessError) => { 209 console.error(`Failed to get ip address. Code: ${err.code}, message: ${err.message}`); 210}); 211``` 212 213## networkManager.getMac 214 215getMac(admin: Want, networkInterface: string, callback: AsyncCallback<string>): void 216 217指定设备管理应用根据网络接口获取设备MAC地址。使用callback异步回调。 218 219**需要权限:** ohos.permission.ENTERPRISE_GET_NETWORK_INFO 220 221**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 222 223**系统API**: 此接口为系统接口。 224 225**参数:** 226 227| 参数名 | 类型 | 必填 | 说明 | 228| -------- | ---------------------------------------- | ---- | ------------------------------- | 229| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 230| networkInterface | string | 是 | 指定网络接口。 | 231| callback | AsyncCallback<string> | 是 | 回调函数。当接口调用成功,err为null,data为设备MAC地址,否则err为错误对象。 | 232 233**错误码**: 234 235以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 236 237| 错误码ID | 错误信息 | 238| ------- | ---------------------------------------------------------------------------- | 239| 9200001 | the application is not an administrator of the device. | 240| 9200002 | the administrator application does not have permission to manage the device. | 241 242**示例:** 243 244```ts 245import Want from '@ohos.app.ability.Want'; 246let wantTemp: Want = { 247 bundleName: 'com.example.myapplication', 248 abilityName: 'EntryAbility', 249}; 250 251networkManager.getMac(wantTemp, 'eth0', (err, result) => { 252 if (err) { 253 console.error(`Failed to get mac. Code: ${err.code}, message: ${err.message}`); 254 return; 255 } 256 console.info(`Succeeded in getting mac, result : ${result}`); 257}); 258``` 259 260## networkManager.getMac 261 262getMac(admin: Want, networkInterface: string): Promise\<string> 263 264指定设备管理应用根据网络接口获取设备MAC地址。使用Promise异步回调。 265 266**需要权限:** ohos.permission.ENTERPRISE_GET_NETWORK_INFO 267 268**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 269 270**系统API**: 此接口为系统接口。 271 272**参数:** 273 274| 参数名 | 类型 | 必填 | 说明 | 275| ----- | ----------------------------------- | ---- | ------- | 276| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 277| networkInterface | string | 是 | 指定网络接口。 | 278 279**返回值:** 280 281| 类型 | 说明 | 282| --------------------- | ------------------------- | 283| Promise<string> | Promise结果,返回设备MAC地址。 | 284 285**错误码**: 286 287以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 288 289| 错误码ID | 错误信息 | 290| ------- | ---------------------------------------------------------------------------- | 291| 9200001 | the application is not an administrator of the device. | 292| 9200002 | the administrator application does not have permission to manage the device. | 293 294**示例:** 295 296```ts 297import Want from '@ohos.app.ability.Want'; 298import { BusinessError } from '@ohos.base'; 299let wantTemp: Want = { 300 bundleName: 'com.example.myapplication', 301 abilityName: 'EntryAbility', 302}; 303 304networkManager.getMac(wantTemp, 'eth0').then((result) => { 305 console.info(`Succeeded in getting mac, result : ${result}`); 306}).catch((err: BusinessError) => { 307 console.error(`Failed to get mac. Code: ${err.code}, message: ${err.message}`); 308}); 309``` 310 311## networkManager.isNetworkInterfaceDisabled 312 313isNetworkInterfaceDisabled(admin: Want, networkInterface: string, callback: AsyncCallback<boolean>): void 314 315指定设备管理应用查询指定网络接口是否被禁用。使用callback异步回调。 316 317**需要权限:** ohos.permission.ENTERPRISE_GET_NETWORK_INFO 318 319**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 320 321**系统API**: 此接口为系统接口。 322 323**参数:** 324 325| 参数名 | 类型 | 必填 | 说明 | 326| -------- | ---------------------------------------- | ---- | ------------------------------- | 327| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 328| networkInterface | string | 是 | 指定网络接口。 | 329| callback | AsyncCallback<boolean> | 是 | 回调函数。当接口调用成功,err为null,data为指定网络接口是否被禁用,true表示该网络接口被禁用,false表示该网络接口未被禁用,否则err为错误对象。 | 330 331**错误码**: 332 333以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 334 335| 错误码ID | 错误信息 | 336| ------- | ---------------------------------------------------------------------------- | 337| 9200001 | the application is not an administrator of the device. | 338| 9200002 | the administrator application does not have permission to manage the device. | 339 340**示例:** 341 342```ts 343import Want from '@ohos.app.ability.Want'; 344let wantTemp: Want = { 345 bundleName: 'com.example.myapplication', 346 abilityName: 'EntryAbility', 347}; 348 349networkManager.isNetworkInterfaceDisabled(wantTemp, 'eth0', (err, result) => { 350 if (err) { 351 console.error(`Failed to query network interface is disabled or not. Code: ${err.code}, message: ${err.message}`); 352 return; 353 } 354 console.info(`Succeeded in querying network interface is disabled or not, result : ${result}`); 355}); 356``` 357 358## networkManager.isNetworkInterfaceDisabled 359 360isNetworkInterfaceDisabled(admin: Want, networkInterface: string): Promise<boolean> 361 362指定设备管理应用查询指定网络接口是否被禁用。使用Promise异步回调。 363 364**需要权限:** ohos.permission.ENTERPRISE_GET_NETWORK_INFO 365 366**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 367 368**系统API**: 此接口为系统接口。 369 370**参数:** 371 372| 参数名 | 类型 | 必填 | 说明 | 373| ----- | ----------------------------------- | ---- | ------- | 374| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 375| networkInterface | string | 是 | 指定网络接口。 | 376 377**返回值:** 378 379| 类型 | 说明 | 380| --------------------- | ------------------------- | 381| Promise<boolean> | Promise结果,返回指定网络接口是否被禁用,true表示该网络接口被禁用,false表示该网络接口未被禁用。 | 382 383**错误码**: 384 385以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 386 387| 错误码ID | 错误信息 | 388| ------- | ---------------------------------------------------------------------------- | 389| 9200001 | the application is not an administrator of the device. | 390| 9200002 | the administrator application does not have permission to manage the device. | 391 392**示例:** 393 394```ts 395import Want from '@ohos.app.ability.Want'; 396import { BusinessError } from '@ohos.base'; 397let wantTemp: Want = { 398 bundleName: 'com.example.myapplication', 399 abilityName: 'EntryAbility', 400}; 401 402networkManager.isNetworkInterfaceDisabled(wantTemp, 'eth0').then((result) => { 403 console.info(`Succeeded in querying network interface is disabled or not, result : ${result}`); 404}).catch((err: BusinessError) => { 405 console.error(`Failed to query network interface is disabled or not. Code: ${err.code}, message: ${err.message}`); 406}); 407``` 408 409## networkManager.setNetworkInterfaceDisabled 410 411setNetworkInterfaceDisabled(admin: Want, networkInterface: string, isDisabled: boolean, callback: AsyncCallback<void>): void 412 413指定设备管理应用禁止设备使用指定网络。使用callback异步回调。 414 415**需要权限:** ohos.permission.ENTERPRISE_SET_NETWORK 416 417**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 418 419**系统API**: 此接口为系统接口。 420 421**参数:** 422 423| 参数名 | 类型 | 必填 | 说明 | 424| -------- | ---------------------------------------- | ---- | ------------------------------- | 425| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 426| networkInterface | string | 是 | 指定网络接口。 | 427| isDisabled | boolean | 是 | true表示禁用该网络接口,false表示开启该网络接口。 | 428| callback | AsyncCallback<void> | 是 | 回调函数。当接口调用成功,err为null,否则err为错误对象。 | 429 430**错误码**: 431 432以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 433 434| 错误码ID | 错误信息 | 435| ------- | ---------------------------------------------------------------------------- | 436| 9200001 | the application is not an administrator of the device. | 437| 9200002 | the administrator application does not have permission to manage the device. | 438 439**示例:** 440 441```ts 442import Want from '@ohos.app.ability.Want'; 443let wantTemp: Want = { 444 bundleName: 'com.example.myapplication', 445 abilityName: 'EntryAbility', 446}; 447 448networkManager.setNetworkInterfaceDisabled(wantTemp, 'eth0', true, (err) => { 449 if (err) { 450 console.error(`Failed to set network interface disabled. Code: ${err.code}, message: ${err.message}`); 451 return; 452 } 453 console.info(`Succeeded in setting network interface disabled`); 454}); 455``` 456 457## networkManager.setNetworkInterfaceDisabled 458 459setNetworkInterfaceDisabled(admin: Want, networkInterface: string, isDisabled: boolean): Promise<void> 460 461指定设备管理应用禁止设备使用指定网络。使用Promise异步回调。 462 463**需要权限:** ohos.permission.ENTERPRISE_SET_NETWORK 464 465**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 466 467**系统API**: 此接口为系统接口。 468 469**参数:** 470 471| 参数名 | 类型 | 必填 | 说明 | 472| ----- | ----------------------------------- | ---- | ------- | 473| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 474| networkInterface | string | 是 | 指定网络接口。 | 475| isDisabled | boolean | 是 | true表示禁用该网络接口,false表示开启该网络接口。 | 476 477**返回值:** 478 479| 类型 | 说明 | 480| --------------------- | ------------------------- | 481| Promise<void> | 无返回结果的Promise对象。当禁用网络接口失败时抛出错误对象 | 482 483**错误码**: 484 485以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 486 487| 错误码ID | 错误信息 | 488| ------- | ---------------------------------------------------------------------------- | 489| 9200001 | the application is not an administrator of the device. | 490| 9200002 | the administrator application does not have permission to manage the device. | 491 492**示例:** 493 494```ts 495import Want from '@ohos.app.ability.Want'; 496import { BusinessError } from '@ohos.base'; 497let wantTemp: Want = { 498 bundleName: 'com.example.myapplication', 499 abilityName: 'EntryAbility', 500}; 501 502networkManager.setNetworkInterfaceDisabled(wantTemp, 'eth0', true).then(() => { 503 console.info(`Succeeded in setting network interface disabled`); 504}).catch((err: BusinessError) => { 505 console.error(`Failed to set network interface disabled. Code: ${err.code}, message: ${err.message}`); 506}); 507``` 508 509## networkManager.setGlobalProxy 510 511setGlobalProxy(admin: Want, httpProxy: connection.HttpProxy, callback: AsyncCallback\<void>): void 512 513指定设备管理应用设置网络全局代理,使用callback异步回调。 514 515**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 516 517**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 518 519**系统API**: 此接口为系统接口。 520 521**参数:** 522 523| 参数名 | 类型 | 必填 | 说明 | 524| -------- | ---------------------------------------- | ---- | ------------------------------- | 525| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 526| httpProxy | [connection.HttpProxy](js-apis-net-connection.md#httpproxy10) | 是 | 网络全局Http代理配置信息。 | 527| callback | AsyncCallback<void> | 是 | 回调函数。当接口调用成功,err为null,否则err为错误对象。 | 528 529**错误码**: 530 531以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 532 533| 错误码ID | 错误信息 | 534| ------- | ---------------------------------------------------------------------------- | 535| 9200001 | the application is not an administrator of the device. | 536| 9200002 | the administrator application does not have permission to manage the device. | 537 538**示例:** 539 540```ts 541import Want from '@ohos.app.ability.Want'; 542import connection from '@ohos.net.connection'; 543let wantTemp: Want = { 544 bundleName: 'com.example.myapplication', 545 abilityName: 'EntryAbility', 546}; 547let exclusionStr: string = "192.168,baidu.com" 548let exclusionArray: Array<string> = exclusionStr.split(','); 549let httpProxy: connection.HttpProxy = { 550 host: "192.168.xx.xxx", 551 port: 8080, 552 exclusionList: exclusionArray 553}; 554 555networkManager.setGlobalProxy(wantTemp, httpProxy, (err) => { 556 if (err) { 557 console.error(`Failed to set network global proxy. Code: ${err.code}, message: ${err.message}`); 558 return; 559 } 560 console.info(`Succeeded in setting network global proxy`); 561}); 562``` 563 564## networkManager.setGlobalProxy 565 566setGlobalProxy(admin: Want, httpProxy: connection.HttpProxy): Promise\<void> 567 568指定设备管理应用设置网络全局代理,使用Promise异步回调。 569 570**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 571 572**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 573 574**系统API**: 此接口为系统接口。 575 576**参数:** 577 578| 参数名 | 类型 | 必填 | 说明 | 579| ----- | ----------------------------------- | ---- | ------- | 580| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 581| httpProxy | [connection.HttpProxy](js-apis-net-connection.md#httpproxy10) | 是 | 网络全局Http代理配置信息。 | 582| isDisabled | boolean | 是 | true表示禁用该网络接口,false表示开启该网络接口。 | 583 584**返回值:** 585 586| 类型 | 说明 | 587| --------------------- | ------------------------- | 588| Promise<void> | 无返回结果的Promise对象。当设置网络全局代理失败时抛出错误对象 | 589 590**错误码**: 591 592以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 593 594| 错误码ID | 错误信息 | 595| ------- | ---------------------------------------------------------------------------- | 596| 9200001 | the application is not an administrator of the device. | 597| 9200002 | the administrator application does not have permission to manage the device. | 598 599**示例:** 600 601```ts 602import Want from '@ohos.app.ability.Want'; 603import { BusinessError } from '@ohos.base'; 604import connection from '@ohos.net.connection'; 605let wantTemp: Want = { 606 bundleName: 'com.example.myapplication', 607 abilityName: 'EntryAbility', 608}; 609let exclusionStr: string = "192.168,baidu.com" 610let exclusionArray: Array<string> = exclusionStr.split(','); 611let httpProxy: connection.HttpProxy = { 612 host: "192.168.xx.xxx", 613 port: 8080, 614 exclusionList: exclusionArray 615}; 616 617networkManager.setGlobalProxy(wantTemp, httpProxy).then(() => { 618 console.info(`Succeeded in setting network global proxy`); 619}).catch((err: BusinessError) => { 620 console.error(`Failed to set network global proxy. Code: ${err.code}, message: ${err.message}`); 621}); 622``` 623 624## networkManager.getGlobalProxy 625 626getGlobalProxy(admin: Want, callback: AsyncCallback\<connection.HttpProxy>): void 627 628指定设备管理应用获取网络全局代理,使用callback异步回调。 629 630**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 631 632**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 633 634**系统API**: 此接口为系统接口。 635 636**参数:** 637 638| 参数名 | 类型 | 必填 | 说明 | 639| -------- | ---------------------------------------- | ---- | ------------------------------- | 640| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 641| callback | AsyncCallback<[connection.HttpProxy](js-apis-net-connection.md#httpproxy10)> | 是 | 回调函数。当接口调用成功,err为null,否则err为错误对象。 | 642 643**错误码**: 644 645以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 646 647| 错误码ID | 错误信息 | 648| ------- | ---------------------------------------------------------------------------- | 649| 9200001 | the application is not an administrator of the device. | 650| 9200002 | the administrator application does not have permission to manage the device. | 651 652**示例:** 653 654```ts 655import Want from '@ohos.app.ability.Want'; 656let wantTemp: Want = { 657 bundleName: 'com.example.myapplication', 658 abilityName: 'EntryAbility', 659}; 660 661networkManager.getGlobalProxy(wantTemp, (err, result) => { 662 if (err) { 663 console.error(`Failed to get network global proxy. Code: ${err.code}, message: ${err.message}`); 664 return; 665 } 666 console.info(`Succeeded in getting network global proxy, result : ${JSON.stringify(result)}`); 667}); 668``` 669 670## networkManager.getGlobalProxy 671 672getGlobalProxy(admin: Want): Promise\<connection.HttpProxy> 673 674指定设备管理应用获取网络全局代理,使用Promise异步回调。 675 676**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 677 678**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 679 680**系统API**: 此接口为系统接口。 681 682**参数:** 683 684| 参数名 | 类型 | 必填 | 说明 | 685| ----- | ----------------------------------- | ---- | ------- | 686| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 687 688**返回值:** 689 690| 类型 | 说明 | 691| --------------------- | ------------------------- | 692| Promise<[connection.HttpProxy](js-apis-net-connection.md#httpproxy10)> | Promise对象,返回网络全局Http代理配置信息。 | 693 694**错误码**: 695 696以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 697 698| 错误码ID | 错误信息 | 699| ------- | ---------------------------------------------------------------------------- | 700| 9200001 | the application is not an administrator of the device. | 701| 9200002 | the administrator application does not have permission to manage the device. | 702 703**示例:** 704 705```ts 706import Want from '@ohos.app.ability.Want'; 707import { BusinessError } from '@ohos.base'; 708let wantTemp: Want = { 709 bundleName: 'com.example.myapplication', 710 abilityName: 'EntryAbility', 711}; 712 713networkManager.getGlobalProxy(wantTemp).then(() => { 714 console.info(`Succeeded in getting network global proxy`); 715}).catch((err: BusinessError) => { 716 console.error(`Failed to get network global proxy. Code: ${err.code}, message: ${err.message}`); 717}); 718``` 719 720## networkManager.addIptablesFilterRule 721 722addIptablesFilterRule(admin: Want, filterRule: AddFilterRule, callback: AsyncCallback\<void>): void 723 724指定设备管理应用为设备添加网络包过滤规则。使用callback异步回调。 725 726**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 727 728**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 729 730**系统API**: 此接口为系统接口。 731 732**参数:** 733 734| 参数名 | 类型 | 必填 | 说明 | 735| -------- | ---------------------------------------- | ---- | ------------------------------- | 736| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 737| filterRule | [AddFilterRule](#addfilterrule) | 是 | 添加网络包过滤规则。 | 738| callback | AsyncCallback<void> | 是 | 回调函数。当接口调用成功,err为null,否则err为错误对象。 | 739 740**错误码**: 741 742以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 743 744| 错误码ID | 错误信息 | 745| ------- | ---------------------------------------------------------------------------- | 746| 9200001 | the application is not an administrator of the device. | 747| 9200002 | the administrator application does not have permission to manage the device. | 748 749**示例:** 750 751```ts 752import Want from '@ohos.app.ability.Want'; 753let wantTemp: Want = { 754 bundleName: 'com.example.myapplication', 755 abilityName: 'EntryAbility', 756}; 757let filterRule: networkManager.AddFilterRule = { 758 "ruleNo": 1, 759 "srcAddr": "192.168.1.1-192.168.255.255", 760 "destAddr": "10.1.1.1", 761 "srcPort": "8080", 762 "destPort": "8080", 763 "uid": "9696", 764 "method": networkManager.AddMethod.APPEND, 765 "direction": networkManager.Direction.OUTPUT, 766 "action": networkManager.Action.DENY, 767 "protocol": networkManager.Protocol.UDP, 768} 769 770networkManager.addIptablesFilterRule(wantTemp, filterRule, (err) => { 771 if (err) { 772 console.error(`Failed to set iptables filter rule. Code: ${err.code}, message: ${err.message}`); 773 return; 774 } 775 console.info(`Succeeded in setting iptables filter rule`); 776}); 777``` 778 779## networkManager.addIptablesFilterRule 780 781addIptablesFilterRule(admin: Want, filterRule: AddFilterRule): Promise\<void> 782 783指定设备管理应用为设备添加网络包过滤规则。使用Promise异步回调。 784 785**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 786 787**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 788 789**系统API**: 此接口为系统接口。 790 791**参数:** 792 793| 参数名 | 类型 | 必填 | 说明 | 794| ----- | ----------------------------------- | ---- | ------- | 795| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 796| filterRule | [AddFilterRule](#addfilterrule) | 是 | 添加网络包过滤规则。 | 797 798**返回值:** 799 800| 类型 | 说明 | 801| --------------------- | ------------------------- | 802| Promise<void> | 无返回结果的Promise对象。当添加网络包过滤规则失败时抛出错误对象。 | 803 804**错误码**: 805 806以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 807 808| 错误码ID | 错误信息 | 809| ------- | ---------------------------------------------------------------------------- | 810| 9200001 | the application is not an administrator of the device. | 811| 9200002 | the administrator application does not have permission to manage the device. | 812 813**示例:** 814 815```ts 816import Want from '@ohos.app.ability.Want'; 817import { BusinessError } from '@ohos.base'; 818let wantTemp: Want = { 819 bundleName: 'com.example.myapplication', 820 abilityName: 'EntryAbility', 821}; 822let filterRule: networkManager.AddFilterRule = { 823 "ruleNo": 1, 824 "srcAddr": "192.168.1.1-192.168.255.255", 825 "destAddr": "10.1.1.1", 826 "srcPort": "8080", 827 "destPort": "8080", 828 "uid": "9696", 829 "method": networkManager.AddMethod.APPEND, 830 "direction": networkManager.Direction.OUTPUT, 831 "action": networkManager.Action.DENY, 832 "protocol": networkManager.Protocol.UDP, 833} 834 835networkManager.addIptablesFilterRule(wantTemp, filterRule).then(() => { 836 console.info(`Succeeded in setting iptables filter rule`); 837}).catch((err: BusinessError) => { 838 console.error(`Failed to set iptables filter rule. Code: ${err.code}, message: ${err.message}`); 839}); 840``` 841 842## networkManager.removeIptablesFilterRule 843 844removeIptablesFilterRule(admin: Want, filterRule: RemoveFilterRule, callback: AsyncCallback\<void>): void 845 846指定设备管理应用移除网络包过滤规则。使用callback异步回调。 847 848**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 849 850**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 851 852**系统API**: 此接口为系统接口。 853 854**参数:** 855 856| 参数名 | 类型 | 必填 | 说明 | 857| -------- | ---------------------------------------- | ---- | ------------------------------- | 858| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 859| filterRule | [RemoveFilterRule](#removefilterrule) | 是 | 移除网络包过滤规则。 | 860| callback | AsyncCallback<void> | 是 | 回调函数。当接口调用成功,err为null,否则err为错误对象。 | 861 862**错误码**: 863 864以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 865 866| 错误码ID | 错误信息 | 867| ------- | ---------------------------------------------------------------------------- | 868| 9200001 | the application is not an administrator of the device. | 869| 9200002 | the administrator application does not have permission to manage the device. | 870 871**示例:** 872 873```ts 874import Want from '@ohos.app.ability.Want'; 875let wantTemp: Want = { 876 bundleName: 'com.example.myapplication', 877 abilityName: 'EntryAbility', 878}; 879let filterRule: networkManager.RemoveFilterRule = { 880 "srcAddr": "192.168.1.1-192.168.255.255", 881 "destAddr": "10.1.1.1", 882 "srcPort": "8080", 883 "destPort": "8080", 884 "uid": "9696", 885 "direction": networkManager.Direction.OUTPUT, 886 "action": networkManager.Action.DENY, 887 "protocol": networkManager.Protocol.UDP, 888} 889 890networkManager.removeIptablesFilterRule(wantTemp, filterRule, (err) => { 891 if (err) { 892 console.error(`Failed to remove iptables filter rule. Code: ${err.code}, message: ${err.message}`); 893 return; 894 } 895 console.info(`Succeeded in removing iptables filter rule`); 896}); 897``` 898 899## networkManager.removeIptablesFilterRule 900 901removeIptablesFilterRule(admin: Want, filterRule: RemoveFilterRule): Promise\<void> 902 903指定设备管理应用移除网络包过滤规则。使用Promise异步回调。 904 905**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 906 907**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 908 909**系统API**: 此接口为系统接口。 910 911**参数:** 912 913| 参数名 | 类型 | 必填 | 说明 | 914| ----- | ----------------------------------- | ---- | ------- | 915| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 916| filterRule | [RemoveFilterRule](#removefilterrule) | 是 | 移除网络包过滤规则。 | 917 918**返回值:** 919 920| 类型 | 说明 | 921| --------------------- | ------------------------- | 922| Promise<void> | 无返回结果的Promise对象。当移除网络包过滤规则失败时抛出错误对象。 | 923 924**错误码**: 925 926以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 927 928| 错误码ID | 错误信息 | 929| ------- | ---------------------------------------------------------------------------- | 930| 9200001 | the application is not an administrator of the device. | 931| 9200002 | the administrator application does not have permission to manage the device. | 932 933**示例:** 934 935```ts 936import Want from '@ohos.app.ability.Want'; 937import { BusinessError } from '@ohos.base'; 938let wantTemp: Want = { 939 bundleName: 'com.example.myapplication', 940 abilityName: 'EntryAbility', 941}; 942let filterRule: networkManager.RemoveFilterRule = { 943 "srcAddr": "192.168.1.1-192.168.255.255", 944 "destAddr": "10.1.1.1", 945 "srcPort": "8080", 946 "destPort": "8080", 947 "uid": "9696", 948 "direction": networkManager.Direction.OUTPUT, 949 "action": networkManager.Action.DENY, 950 "protocol": networkManager.Protocol.UDP, 951} 952 953networkManager.removeIptablesFilterRule(wantTemp, filterRule).then(() => { 954 console.info(`Succeeded in removing iptables filter rule`); 955}).catch((err: BusinessError) => { 956 console.error(`Failed to remove iptables filter rule. Code: ${err.code}, message: ${err.message}`); 957}); 958``` 959 960## networkManager.listIptablesFilterRules 961 962listIptablesFilterRules(admin: Want, callback: AsyncCallback\<string>): void 963 964指定设备管理应用获取网络包过滤规则。使用callback异步回调。 965 966**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 967 968**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 969 970**系统API**: 此接口为系统接口。 971 972**参数:** 973 974| 参数名 | 类型 | 必填 | 说明 | 975| -------- | ---------------------------------------- | ---- | ------------------------------- | 976| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 977| callback | AsyncCallback<string> | 是 | 回调函数。当接口调用成功,err为null,否则err为错误对象。 | 978 979**错误码**: 980 981以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 982 983| 错误码ID | 错误信息 | 984| ------- | ---------------------------------------------------------------------------- | 985| 9200001 | the application is not an administrator of the device. | 986| 9200002 | the administrator application does not have permission to manage the device. | 987 988**示例:** 989 990```ts 991import Want from '@ohos.app.ability.Want'; 992let wantTemp: Want = { 993 bundleName: 'com.example.myapplication', 994 abilityName: 'EntryAbility', 995}; 996 997networkManager.listIptablesFilterRules(wantTemp, (err, result) => { 998 if (err) { 999 console.error(`Failed to get iptables filter rule. Code: ${err.code}, message: ${err.message}`); 1000 return; 1001 } 1002 console.info(`Succeeded in getting iptables filter rule, result : ${result}`); 1003}); 1004``` 1005 1006## networkManager.listIptablesFilterRules 1007 1008listIptablesFilterRules(admin: Want): Promise\<string> 1009 1010指定设备管理应用获取网络包过滤规则。使用Promise异步回调。 1011 1012**需要权限:** ohos.permission.ENTERPRISE_MANAGE_NETWORK 1013 1014**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 1015 1016**系统API**: 此接口为系统接口。 1017 1018**参数:** 1019 1020| 参数名 | 类型 | 必填 | 说明 | 1021| ----- | ----------------------------------- | ---- | ------- | 1022| admin | [Want](js-apis-app-ability-want.md) | 是 | 设备管理应用。 | 1023 1024**返回值:** 1025 1026| 类型 | 说明 | 1027| --------------------- | ------------------------- | 1028| Promise<string> | Promise对象,返回网络包过滤规则。 | 1029 1030**错误码**: 1031 1032以下错误码的详细介绍请参见[企业设备管理错误码](../errorcodes/errorcode-enterpriseDeviceManager.md) 1033 1034| 错误码ID | 错误信息 | 1035| ------- | ---------------------------------------------------------------------------- | 1036| 9200001 | the application is not an administrator of the device. | 1037| 9200002 | the administrator application does not have permission to manage the device. | 1038 1039**示例:** 1040 1041```ts 1042import Want from '@ohos.app.ability.Want'; 1043import { BusinessError } from '@ohos.base'; 1044let wantTemp: Want = { 1045 bundleName: 'com.example.myapplication', 1046 abilityName: 'EntryAbility', 1047}; 1048 1049networkManager.listIptablesFilterRules(wantTemp).then((result) => { 1050 console.info(`Succeeded in getting iptables filter rule, result: ${result}`); 1051}).catch((err: BusinessError) => { 1052 console.error(`Failed to remove iptables filter rule. Code: ${err.code}, message: ${err.message}`); 1053}); 1054``` 1055 1056## AddFilterRule 1057 1058添加网络包过滤规则 1059 1060**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 1061 1062**系统API**: 此接口为系统接口。 1063 1064| 名称 | 类型 | 必填 | 说明 | 1065| ----------- | --------| ---- | ------------------------------- | 1066| ruleNo | number | 否 | 规则序号。 | 1067| srcAddr | string | 否 | ip源地址。 | 1068| destAddr | string | 否 | ip目标地址。 | 1069| srcPort | string | 否 | ip源端口。 | 1070| destPort | string | 否 | ip目标端口。 | 1071| uid | string | 否 | 应用uid。 | 1072| method | [AddMethod](#addmethod) | 是 | 添加策略。 | 1073| direction | [Direction](#direction) | 是 | 规则链。 | 1074| action | [Action](#action) | 是 | 接收或者丢弃数据包。 | 1075| protocol | [Protocol](#protocol) | 否 | 网络协议。 | 1076 1077## RemoveFilterRule 1078 1079移除网络包过滤规则 1080 1081**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 1082 1083**系统API**: 此接口为系统接口。 1084 1085| 名称 | 类型 | 必填 | 说明 | 1086| ----------- | --------| ---- | ------------------------------- | 1087| srcAddr | string | 否 | ip源地址。 | 1088| destAddr | string | 否 | ip目标地址。 | 1089| srcPort | string | 否 | ip源端口。 | 1090| destPort | string | 否 | ip目标端口。 | 1091| uid | string | 否 | 应用uid。 | 1092| direction | [Direction](#direction) | 是 | 规则链。 | 1093| action | [Action](#action) | 否 | 接收或者丢弃数据包。 | 1094| protocol | [Protocol](#protocol) | 否 | 网络协议。 | 1095 1096## AddMethod 1097 1098添加网络包方法。 1099 1100**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 1101 1102**系统API**: 此接口为系统接口。 1103 1104| 名称 | 值 | 说明 | 1105| -------- | -------- | -------- | 1106| APPEND | 0 | 追加。 | 1107| INSERT | 1 | 插入。 | 1108 1109## Direction 1110 1111规则链。 1112 1113**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 1114 1115**系统API**: 此接口为系统接口。 1116 1117| 名称 | 值 | 说明 | 1118| -------- | -------- | -------- | 1119| INPUT | 0 | 输入链。 | 1120| OUTPUT | 1 | 输出链。 | 1121 1122## Action 1123 1124数据包的行为。 1125 1126**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 1127 1128**系统API**: 此接口为系统接口。 1129 1130| 名称 | 值 | 说明 | 1131| -------- | -------- | -------- | 1132| ALLOW | 0 | 接收数据包。 | 1133| DENY | 1 | 丢弃数据包。 | 1134 1135## Protocol 1136 1137网络协议。 1138 1139**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager 1140 1141**系统API**: 此接口为系统接口。 1142 1143| 名称 | 值 | 说明 | 1144| -------- | -------- | -------- | 1145| ALL | 0 | 全部网络协议。 | 1146| TCP | 1 | 网络协议TCP。 | 1147| UDP | 2 | 网络协议UDP。 | 1148| ICMP | 3 | 网络协议ICMP。 | 1149