1# @ohos.net.connection (Network Connection Management) 2 3The 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. 4 5> **NOTE** 6> 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. 7 8## Modules to Import 9 10```ts 11import connection from '@ohos.net.connection' 12``` 13 14## connection.createNetConnection<sup>8+</sup> 15 16createNetConnection(netSpecifier?: NetSpecifier, timeout?: number): NetConnection 17 18Creates a **NetConnection** object. **netSpecifier** specifies the network, and **timeout** specifies the timeout duration in ms. **timeout** is configurable only when **netSpecifier** is specified. If neither of them is present, the default network is used. 19 20**System capability**: SystemCapability.Communication.NetManager.Core 21 22**Parameters** 23 24| Name | Type | Mandatory| Description | 25| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ | 26| netSpecifier | [NetSpecifier](#netspecifier) | No | Network specifier, which specifies the characteristics of a network. If this parameter is not set or is set to **undefined**, the default network is used. | 27| timeout | number | No | Timeout duration for obtaining the network specified by **netSpecifier**. This parameter is valid only when **netSpecifier** is specified. The default value is **0** if **netSpecifier** is **undefined**.| 28 29**Return value** 30 31| Type | Description | 32| ------------------------------- | -------------------- | 33| [NetConnection](#netconnection) | Handle of the network specified by **netSpecifier**.| 34 35**Example** 36 37```ts 38import connection from '@ohos.net.connection' 39 40// For the default network, you do not need to pass in parameters. 41let netConnection = connection.createNetConnection() 42 43// For the cellular network, you need to pass in related network parameters. If the timeout parameter is not specified, the timeout value is 0 by default. 44let netConnectionCellular = connection.createNetConnection({ 45 netCapabilities: { 46 bearerTypes: [connection.NetBearType.BEARER_CELLULAR] 47 } 48}) 49``` 50 51## connection.getDefaultNet<sup>8+</sup> 52 53getDefaultNet(callback: AsyncCallback\<NetHandle>): void 54 55Obtains the default active data network. This API uses an asynchronous callback to return the result. You can use [getNetCapabilities](#connectiongetnetcapabilities) to obtain information such as the network type and capabilities. 56 57**Required permission**: ohos.permission.GET_NETWORK_INFO 58 59**System capability**: SystemCapability.Communication.NetManager.Core 60 61**Parameters** 62 63| Name | Type | Mandatory| Description | 64| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 65| callback | AsyncCallback\<[NetHandle](#nethandle)> | Yes | Callback used to return the result. If the default activated data network is obtained successfully, **error** is **undefined** and **data** is the default activated data network. Otherwise, **error** is an error object.| 66 67**Error codes** 68 69| ID| Error Message | 70| ------- | ----------------------------- | 71| 201 | Permission denied. | 72| 401 | Parameter error. | 73| 2100002 | Operation failed. Cannot connect to service.| 74| 2100003 | System internal error. | 75 76**Example** 77 78```ts 79import connection from '@ohos.net.connection' 80import { BusinessError } from '@ohos.base' 81 82connection.getDefaultNet((error: BusinessError, data: connection.NetHandle) => { 83 console.log(JSON.stringify(error)) 84 console.log(JSON.stringify(data)) 85}) 86``` 87 88## connection.getDefaultNet<sup>8+</sup> 89 90getDefaultNet(): Promise\<NetHandle> 91 92Obtains the default active data network. This API uses a promise to return the result. You can use [getNetCapabilities](#connectiongetnetcapabilities) to obtain information such as the network type and capabilities. 93 94**Required permission**: ohos.permission.GET_NETWORK_INFO 95 96**System capability**: SystemCapability.Communication.NetManager.Core 97 98**Return value** 99 100| Type | Description | 101| --------------------------------- | ------------------------------------- | 102| Promise\<[NetHandle](#nethandle)> | Promise used to return the result.| 103 104**Error codes** 105 106| ID| Error Message | 107| ------- | ----------------------------- | 108| 201 | Permission denied. | 109| 401 | Parameter error. | 110| 2100002 | Operation failed. Cannot connect to service.| 111| 2100003 | System internal error. | 112 113**Example** 114 115```ts 116import connection from '@ohos.net.connection' 117connection.getDefaultNet().then((data: connection.NetHandle) => { 118 console.log(JSON.stringify(data)) 119}) 120``` 121 122## connection.getDefaultNetSync<sup>9+</sup> 123 124getDefaultNetSync(): NetHandle 125 126Obtains the default active data network in synchronous mode. You can use [getNetCapabilities](#connectiongetnetcapabilities) to obtain information such as the network type and capabilities. 127 128**Required permission**: ohos.permission.GET_NETWORK_INFO 129 130**System capability**: SystemCapability.Communication.NetManager.Core 131 132**Return value** 133 134| Type | Description | 135| --------- | ---------------------------------- | 136| NetHandle | Handle of the default active data network.| 137 138**Error codes** 139 140| ID| Error Message | 141| ------- | ----------------------------- | 142| 201 | Permission denied. | 143| 401 | Parameter error. | 144| 2100002 | Operation failed. Cannot connect to service.| 145| 2100003 | System internal error. | 146 147**Example** 148 149```ts 150import connection from '@ohos.net.connection' 151 152let netHandle = connection.getDefaultNetSync(); 153``` 154 155## connection.getGlobalHttpProxy<sup>10+</sup> 156 157getGlobalHttpProxy(callback: AsyncCallback\<HttpProxy>): void 158 159Obtains the global HTTP proxy configuration of the network. This API uses an asynchronous callback to return the result. 160 161**System API**: This is a system API. 162 163**System capability**: SystemCapability.Communication.NetManager.Core 164 165**Parameters** 166 167| Name | Type | Mandatory| Description | 168| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 169| callback | AsyncCallback\<[HttpProxy](#httpproxy10)> | 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.| 170 171**Error codes** 172 173| ID| Error Message | 174| ------- | ----------------------------- | 175| 401 | Parameter error. | 176| 202 | Non-system applications use system APIs. | 177| 2100002 | Operation failed. Cannot connect to service.| 178| 2100003 | System internal error. | 179 180**Example** 181 182```ts 183import connection from '@ohos.net.connection' 184import { BusinessError } from '@ohos.base' 185 186connection.getGlobalHttpProxy((error: BusinessError, data: connection.HttpProxy) => { 187 console.info(JSON.stringify(error)); 188 console.info(JSON.stringify(data)); 189}) 190``` 191 192## connection.getGlobalHttpProxy<sup>10+</sup> 193 194getGlobalHttpProxy(): Promise\<HttpProxy>; 195 196Obtains the global HTTP proxy configuration of the network. This API uses a promise to return the result. 197 198**System API**: This is a system API. 199 200**System capability**: SystemCapability.Communication.NetManager.Core 201 202**Return value** 203 204| Type | Description | 205| --------------------------------- | ------------------------------------- | 206| Promise\<[HttpProxy](#httpproxy10)> | Promise used to return the result.| 207 208**Error codes** 209 210| ID| Error Message | 211| ------- | ----------------------------- | 212| 401 | Parameter error. | 213| 202 | Non-system applications use system APIs. | 214| 2100002 | Operation failed. Cannot connect to service.| 215| 2100003 | System internal error. | 216 217**Example** 218 219```ts 220import connection from '@ohos.net.connection' 221import { BusinessError } from '@ohos.base' 222 223connection.getGlobalHttpProxy().then((data: connection.HttpProxy) => { 224 console.info(JSON.stringify(data)); 225}).catch((error: BusinessError) => { 226 console.info(JSON.stringify(error)); 227}) 228``` 229 230## connection.setGlobalHttpProxy<sup>10+</sup> 231 232setGlobalHttpProxy(httpProxy: HttpProxy, callback: AsyncCallback\<void>): void 233 234Sets the global HTTP proxy configuration of the network. This API uses an asynchronous callback to return the result. 235 236**System API**: This is a system API. 237 238**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 239 240**System capability**: SystemCapability.Communication.NetManager.Core 241 242**Parameters** 243 244| Name | Type | Mandatory| Description | 245| --------- | ----------------------- | ---- | ------------------------------------------------------------ | 246| httpProxy | [HttpProxy](#httpproxy10) | Yes | Global HTTP proxy configuration of the network. | 247| 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.| 248 249**Error codes** 250 251| ID| Error Message | 252| ------- | ----------------------------- | 253| 201 | Permission denied. | 254| 401 | Parameter error. | 255| 202 | Non-system applications use system APIs. | 256| 2100001 | Invalid parameter value. | 257| 2100002 | Operation failed. Cannot connect to service.| 258| 2100003 | System internal error. | 259 260**Example** 261 262```ts 263import connection from '@ohos.net.connection' 264import { BusinessError } from '@ohos.base'; 265 266let exclusionStr = "192.168,baidu.com" 267let exclusionArray = exclusionStr.split(','); 268connection.setGlobalHttpProxy({ 269 host: "192.168.xx.xxx", 270 port: 8080, 271 exclusionList: exclusionArray 272} as connection.HttpProxy).then(() => { 273 console.info("success"); 274}).catch((error: BusinessError) => { 275 console.info(JSON.stringify(error)); 276}); 277``` 278 279## connection.setGlobalHttpProxy<sup>10+</sup> 280 281setGlobalHttpProxy(httpProxy: HttpProxy): Promise\<void>; 282 283Sets the global HTTP proxy configuration of the network. This API uses a promise to return the result. 284 285**System API**: This is a system API. 286 287**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 288 289**System capability**: SystemCapability.Communication.NetManager.Core 290 291**Parameters** 292 293| Name | Type | Mandatory| Description | 294| --------- | ------------------------------------------------------------ | ---- | ---------------- | 295| httpProxy | [HttpProxy](#httpproxy10) | Yes | Global HTTP proxy configuration of the network.| 296 297**Return value** 298 299| Type | Description | 300| ------------------------------------------- | ----------------------------- | 301| Promise\<void> | Promise that returns no value.| 302 303**Error codes** 304 305| ID| Error Message | 306| ------- | ----------------------------- | 307| 201 | Permission denied. | 308| 401 | Parameter error. | 309| 202 | Non-system applications use system APIs. | 310| 2100001 | Invalid parameter value. | 311| 2100002 | Operation failed. Cannot connect to service.| 312| 2100003 | System internal error. | 313 314**Example** 315 316```ts 317import connection from '@ohos.net.connection' 318import { BusinessError } from '@ohos.base'; 319 320let exclusionStr = "192.168,baidu.com" 321let exclusionArray = exclusionStr.split(','); 322connection.setGlobalHttpProxy({ 323 host: "192.168.xx.xxx", 324 port: 8080, 325 exclusionList: exclusionArray 326} as connection.HttpProxy).then(() => { 327 console.info("success"); 328}).catch((error: BusinessError) => { 329 console.info(JSON.stringify(error)); 330}); 331``` 332 333## connection.getDefaultHttpProxy<sup>10+</sup> 334 335getDefaultHttpProxy(callback: AsyncCallback\<HttpProxy>): void 336 337Obtains the default HTTP proxy configuration of the network. 338If the global proxy is set, the global HTTP proxy configuration is returned. If [setAppNet](#connectionsetappnet) is used to bind the application to the network specified by [NetHandle](#nethandle), the HTTP proxy configuration of this network is returned. In other cases, the HTTP proxy configuration of the default network is returned. 339This API uses an asynchronous callback to return the result. 340 341**System capability**: SystemCapability.Communication.NetManager.Core 342 343**Parameters** 344 345| Name | Type | Mandatory| Description | 346| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 347| callback | AsyncCallback<[HttpProxy](#httpproxy10)> | 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.| 348 349**Error codes** 350 351| ID| Error Message | 352| -------- | -------------------------------------------- | 353| 2100002 | Operation failed. Cannot connect to service. | 354| 2100003 | System internal error. | 355 356**Example** 357 358```ts 359import connection from '@ohos.net.connection' 360import { BusinessError } from '@ohos.base' 361 362connection.getDefaultHttpProxy((error: BusinessError, data: connection.HttpProxy) => { 363 console.info(JSON.stringify(error)); 364 console.info(JSON.stringify(data)); 365}) 366``` 367 368## connection.getDefaultHttpProxy<sup>10+</sup> 369 370getDefaultHttpProxy(): Promise\<HttpProxy>; 371 372Obtains the default HTTP proxy configuration of the network. 373If the global proxy is set, the global HTTP proxy configuration is returned. If [setAppNet](#connectionsetappnet) is used to bind the application to the network specified by [NetHandle](#nethandle), the HTTP proxy configuration of this network is returned. In other cases, the HTTP proxy configuration of the default network is returned. 374This API uses a promise to return the result. 375 376**System capability**: SystemCapability.Communication.NetManager.Core 377 378**Return value** 379 380| Type | Description | 381| -------------------------------- | ----------------------------------------- | 382| Promise<[HttpProxy](#httpproxy10)> | Promise used to return the result.| 383 384**Error codes** 385 386| ID| Error Message | 387| -------- | -------------------------------------------- | 388| 2100002 | Operation failed. Cannot connect to service. | 389| 2100003 | System internal error. | 390 391**Example** 392 393```ts 394import connection from '@ohos.net.connection' 395import { BusinessError } from '@ohos.base' 396 397connection.getDefaultHttpProxy().then((data: connection.HttpProxy) => { 398 console.info(JSON.stringify(data)); 399}).catch((error: BusinessError) => { 400 console.info(JSON.stringify(error)); 401}) 402``` 403 404## connection.getAppNet<sup>9+</sup> 405 406getAppNet(callback: AsyncCallback\<NetHandle>): void 407 408Obtains information about the network bound to an application. This API uses an asynchronous callback to return the result. 409 410**System capability**: SystemCapability.Communication.NetManager.Core 411 412**Parameters** 413 414| Name | Type | Mandatory| Description | 415| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 416| callback | AsyncCallback\<[NetHandle](#nethandle)> | Yes | Callback used to return the result. If information about the network bound to the application is successfully obtained, **error** is **undefined** and **data** is the obtained network information. Otherwise, **error** is an error object.| 417 418**Error codes** 419 420| ID| Error Message | 421| ------- | ----------------------------- | 422| 401 | Parameter error.| 423| 2100002 | Operation failed. Cannot connect to service.| 424| 2100003 | System internal error. | 425 426**Example** 427 428```ts 429import connection from '@ohos.net.connection' 430import { BusinessError } from '@ohos.base' 431 432connection.getAppNet((error: BusinessError, data: connection.NetHandle) => { 433 console.log(JSON.stringify(error)) 434 console.log(JSON.stringify(data)) 435}) 436``` 437 438## connection.getAppNet<sup>9+</sup> 439 440getAppNet(): Promise\<NetHandle>; 441 442Obtains information about the network bound to an application. This API uses a promise to return the result. 443 444**System capability**: SystemCapability.Communication.NetManager.Core 445 446**Return value** 447 448| Type | Description | 449| --------------------------------- | ------------------------------------- | 450| Promise\<[NetHandle](#nethandle)> | Promise used to return the result.| 451 452**Error codes** 453 454| ID| Error Message | 455| ------- | ----------------------------- | 456| 401 | Parameter error.| 457| 2100002 | Operation failed. Cannot connect to service.| 458| 2100003 | System internal error. | 459 460**Example** 461 462```ts 463import connection from '@ohos.net.connection' 464import { BusinessError } from '@ohos.base' 465 466connection.getAppNet().then((data: connection.NetHandle) => { 467 console.info(JSON.stringify(data)); 468}).catch((error: BusinessError) => { 469 console.info(JSON.stringify(error)); 470}) 471``` 472 473## connection.getAppNetSync<sup>10+</sup> 474 475getAppNetSync(): NetHandle 476 477Obtains information about the network bound to an application. This API returns the result synchronously. 478 479**System capability**: SystemCapability.Communication.NetManager.Core 480 481**Return value** 482 483| Type | Description | 484| --------- | ---------------------------------- | 485| [NetHandle](#nethandle8) | Handle of the data network bound to the application.| 486 487**Error codes** 488 489| ID| Error Message | 490| ------- | ----------------------------- | 491| 401 | Parameter error. | 492| 2100002 | Operation failed. Cannot connect to service.| 493| 2100003 | System internal error. | 494 495**Example** 496 497```ts 498import connection from '@ohos.net.connection' 499 500let netHandle = connection.getAppNetSync(); 501``` 502 503## connection.SetAppNet<sup>9+</sup> 504 505setAppNet(netHandle: NetHandle, callback: AsyncCallback\<void>): void 506 507Binds an application to the specified network, so that the application can access the external network only through this network. This API uses an asynchronous callback to return the result. 508 509**Required permissions**: ohos.permission.INTERNET 510 511**System capability**: SystemCapability.Communication.NetManager.Core 512 513**Parameters** 514 515| Name | Type | Mandatory| Description | 516| --------- | ----------------------- | ---- | ------------------------------------------------------------ | 517| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network. | 518| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the application is successfully bound to the specified network, **error** is **undefined**. Otherwise, **error** is an error object.| 519 520**Error codes** 521 522| ID| Error Message | 523| ------- | ----------------------------- | 524| 201 | Permission denied. | 525| 401 | Parameter error. | 526| 2100001 | Invalid parameter value. | 527| 2100002 | Operation failed. Cannot connect to service.| 528| 2100003 | System internal error. | 529 530**Example** 531 532```ts 533import connection from '@ohos.net.connection' 534import { BusinessError } from '@ohos.base' 535 536connection.getDefaultNet((error: BusinessError, netHandle: connection.NetHandle) => { 537 connection.setAppNet(netHandle, (error: BusinessError, data: void) => { 538 console.log(JSON.stringify(error)) 539 console.log(JSON.stringify(data)) 540 }); 541}) 542``` 543 544## connection.SetAppNet<sup>9+</sup> 545 546setAppNet(netHandle: NetHandle): Promise\<void>; 547 548Binds an application to the specified network, so that the application can access the external network only through this network. This API uses a promise to return the result. 549 550**Required permissions**: ohos.permission.INTERNET 551 552**System capability**: SystemCapability.Communication.NetManager.Core 553 554**Parameters** 555 556| Name | Type | Mandatory| Description | 557| --------- | ------------------------------------------------------------ | ---- | ---------------- | 558| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 559 560**Return value** 561 562| Type | Description | 563| ------------------------------------------- | ----------------------------- | 564| Promise\<void> | Promise that returns no value.| 565 566**Error codes** 567 568| ID| Error Message | 569| ------- | ----------------------------- | 570| 201 | Permission denied. | 571| 401 | Parameter error. | 572| 2100001 | Invalid parameter value. | 573| 2100002 | Operation failed. Cannot connect to service.| 574| 2100003 | System internal error. | 575 576**Example** 577 578```ts 579import connection from '@ohos.net.connection' 580import { BusinessError } from '@ohos.base' 581 582connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 583 connection.setAppNet(netHandle).then(() => { 584 console.log("success") 585 }).catch((error: BusinessError) => { 586 console.log(JSON.stringify(error)) 587 }) 588}) 589``` 590 591## connection.getAllNets<sup>8+</sup> 592 593getAllNets(callback: AsyncCallback<Array<NetHandle>>): void 594 595Obtains the list of all connected networks. This API uses an asynchronous callback to return the result. 596 597**Required permission**: ohos.permission.GET_NETWORK_INFO 598 599**System capability**: SystemCapability.Communication.NetManager.Core 600 601**Parameters** 602 603| Name| Type| Mandatory| Description| 604| -------- | -------- | -------- | -------- | 605| callback | AsyncCallback<Array<[NetHandle](#nethandle)>> | Yes| Callback used to return the result. If the list of all connected networks is obtained successfully, **error** is **undefined** and **data** is the list of activated data networks. Otherwise, **error** is an error object.| 606 607**Error codes** 608 609| ID| Error Message | 610| ------- | ----------------------------- | 611| 201 | Permission denied. | 612| 401 | Parameter error. | 613| 2100002 | Operation failed. Cannot connect to service.| 614| 2100003 | System internal error. | 615 616**Example** 617 618```ts 619import connection from '@ohos.net.connection' 620import { BusinessError } from '@ohos.base' 621 622connection.getAllNets((error: BusinessError, data: connection.NetHandle[]) => { 623 console.log(JSON.stringify(error)) 624 console.log(JSON.stringify(data)) 625}); 626``` 627 628## connection.getAllNets<sup>8+</sup> 629 630getAllNets(): Promise<Array<NetHandle>> 631 632Obtains the list of all connected networks. This API uses a promise to return the result. 633 634**Required permission**: ohos.permission.GET_NETWORK_INFO 635 636**System capability**: SystemCapability.Communication.NetManager.Core 637 638**Return value** 639 640| Type| Description| 641| -------- | -------- | 642| Promise<Array<[NetHandle](#nethandle)>> | Promise used to return the result.| 643 644**Error codes** 645 646| ID| Error Message | 647| ------- | ----------------------------- | 648| 201 | Permission denied. | 649| 401 | Parameter error. | 650| 2100002 | Operation failed. Cannot connect to service.| 651| 2100003 | System internal error. | 652 653**Example** 654 655```ts 656import connection from '@ohos.net.connection' 657 658connection.getAllNets().then((data: connection.NetHandle[]) => { 659 console.log(JSON.stringify(data)) 660}); 661``` 662 663## connection.getAllNetsSync<sup>10+</sup> 664 665getAllNetsSync(): Array<NetHandle> 666 667Obtains the list of all connected networks. This API returns the result synchronously. 668 669**Required permission**: ohos.permission.GET_NETWORK_INFO 670 671**System capability**: SystemCapability.Communication.NetManager.Core 672 673**Return value** 674 675| Type | Description | 676| --------- | ---------------------------------- | 677| Array<[NetHandle](#nethandle8)> | List of all activated data networks.| 678 679**Error codes** 680 681| ID| Error Message | 682| ------- | ----------------------------- | 683| 201 | Permission denied. | 684| 401 | Parameter error. | 685| 2100002 | Operation failed. Cannot connect to service.| 686| 2100003 | System internal error. | 687 688**Example** 689 690```ts 691import connection from '@ohos.net.connection' 692 693let netHandle = connection.getAllNetsSync(); 694``` 695 696## connection.getConnectionProperties<sup>8+</sup> 697 698getConnectionProperties(netHandle: NetHandle, callback: AsyncCallback\<ConnectionProperties>): void 699 700Obtains connection properties of the network corresponding to the **netHandle**. This API uses an asynchronous callback to return the result. 701 702**Required permission**: ohos.permission.GET_NETWORK_INFO 703 704**System capability**: SystemCapability.Communication.NetManager.Core 705 706**Parameters** 707 708| Name | Type | Mandatory| Description | 709| --------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 710| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network. | 711| callback | AsyncCallback\<[ConnectionProperties](#connectionproperties)> | Yes | Callback used to return the result. If the connection properties of the network corresponding to the **netHandle** is obtained successfully, **error** is **undefined** and **data** is the obtained network connection information. Otherwise, **error** is an error object.| 712 713**Error codes** 714 715| ID| Error Message | 716| ------- | ----------------------------- | 717| 201 | Permission denied. | 718| 401 | Parameter error. | 719| 2100001 | Invalid parameter value. | 720| 2100002 | Operation failed. Cannot connect to service.| 721| 2100003 | System internal error. | 722 723**Example** 724 725```ts 726import connection from '@ohos.net.connection' 727import { BusinessError } from '@ohos.base' 728 729connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 730 connection.getConnectionProperties(netHandle, (error: BusinessError, data: connection.ConnectionProperties) => { 731 console.log(JSON.stringify(error)) 732 console.log(JSON.stringify(data)) 733 }) 734}) 735``` 736 737## connection.getConnectionProperties<sup>8+</sup> 738 739getConnectionProperties(netHandle: NetHandle): Promise\<ConnectionProperties> 740 741Obtains connection properties of the network corresponding to the **netHandle**. This API uses a promise to return the result. 742 743**Required permission**: ohos.permission.GET_NETWORK_INFO 744 745**System capability**: SystemCapability.Communication.NetManager.Core 746 747**Parameters** 748 749| Name | Type | Mandatory| Description | 750| --------- | ----------------------- | ---- | ---------------- | 751| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 752 753**Return value** 754 755| Type | Description | 756| ------------------------------------------------------- | --------------------------------- | 757| Promise\<[ConnectionProperties](#connectionproperties)> | Promise used to return the result.| 758 759**Error codes** 760 761| ID| Error Message | 762| ------- | ----------------------------- | 763| 201 | Permission denied. | 764| 401 | Parameter error. | 765| 2100001 | Invalid parameter value. | 766| 2100002 | Operation failed. Cannot connect to service.| 767| 2100003 | System internal error. | 768 769**Example** 770 771```ts 772import connection from '@ohos.net.connection' 773 774connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 775 connection.getConnectionProperties(netHandle).then((data: connection.ConnectionProperties) => { 776 console.log(JSON.stringify(data)) 777 }) 778}) 779``` 780 781## connection.getConnectionPropertiesSync<sup>10+</sup> 782 783getConnectionPropertiesSync(netHandle: NetHandle): ConnectionProperties 784 785Obtains network connection information based on the specified **netHandle**. 786 787**Required permission**: ohos.permission.GET_NETWORK_INFO 788 789**System capability**: SystemCapability.Communication.NetManager.Core 790 791**Parameters** 792 793| Name | Type | Mandatory| Description | 794| --------- | ----------------------- | ---- | ---------------- | 795| netHandle | [NetHandle](#nethandle8) | Yes | Handle of the data network.| 796 797**Return value** 798 799| Type | Description | 800| ------------------------------------------------------- | --------------------------------- | 801| [ConnectionProperties](#connectionproperties8) | Network connection information.| 802 803**Error codes** 804 805| ID| Error Message | 806| ------- | ----------------------------- | 807| 201 | Permission denied. | 808| 401 | Parameter error. | 809| 2100001 | Invalid parameter value. | 810| 2100002 | Operation failed. Cannot connect to service.| 811| 2100003 | System internal error. | 812 813**Example** 814 815```ts 816import connection from '@ohos.net.connection' 817 818let netHandle = connection.getDefaultNetSync(); 819let connectionproperties = connection.getConnectionPropertiesSync(netHandle); 820``` 821 822## connection.getNetCapabilities<sup>8+</sup> 823 824getNetCapabilities(netHandle: NetHandle, callback: AsyncCallback\<NetCapabilities>): void 825 826Obtains capability information of the network corresponding to the **netHandle**. This API uses an asynchronous callback to return the result. 827 828**Required permission**: ohos.permission.GET_NETWORK_INFO 829 830**System capability**: SystemCapability.Communication.NetManager.Core 831 832**Parameters** 833 834| Name | Type | Mandatory| Description | 835| --------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ | 836| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network. | 837| callback | AsyncCallback\<[NetCapabilities](#netcapabilities)> | Yes | Callback used to return the result. If the capability information of the network corresponding to the **netHandle** is obtained successfully, **error** is **undefined** and **data** is the obtained network capability information. Otherwise, **error** is an error object.| 838 839**Error codes** 840 841| ID| Error Message | 842| ------- | ----------------------------- | 843| 201 | Permission denied. | 844| 401 | Parameter error. | 845| 2100001 | Invalid parameter value. | 846| 2100002 | Operation failed. Cannot connect to service.| 847| 2100003 | System internal error. | 848 849**Example** 850 851```ts 852import connection from '@ohos.net.connection' 853import { BusinessError } from '@ohos.base' 854 855connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 856 connection.getNetCapabilities(netHandle, (error: BusinessError, data: connection.NetCapabilities) => { 857 console.log(JSON.stringify(error)) 858 console.log(JSON.stringify(data)) 859 }) 860}) 861``` 862 863## connection.getNetCapabilities<sup>8+</sup> 864 865getNetCapabilities(netHandle: NetHandle): Promise\<NetCapabilities> 866 867Obtains capability information of the network corresponding to the **netHandle**. This API uses a promise to return the result. 868 869**Required permission**: ohos.permission.GET_NETWORK_INFO 870 871**System capability**: SystemCapability.Communication.NetManager.Core 872 873**Parameters** 874 875| Name | Type | Mandatory| Description | 876| --------- | ----------------------- | ---- | ---------------- | 877| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network.| 878 879**Return value** 880 881| Type | Description | 882| --------------------------------------------- | --------------------------------- | 883| Promise\<[NetCapabilities](#netcapabilities)> | Promise used to return the result.| 884 885**Error codes** 886 887| ID| Error Message | 888| ------- | ----------------------------- | 889| 201 | Permission denied. | 890| 401 | Parameter error. | 891| 2100001 | Invalid parameter value. | 892| 2100002 | Operation failed. Cannot connect to service.| 893| 2100003 | System internal error. | 894 895**Example** 896 897```ts 898import connection from '@ohos.net.connection' 899 900connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 901 connection.getNetCapabilities(netHandle).then((data: connection.NetCapabilities) => { 902 console.log(JSON.stringify(data)) 903 }) 904}) 905``` 906 907## connection.getNetCapabilitiesSync<sup>10+</sup> 908 909getNetCapabilitiesSync(netHandle: NetHandle): NetCapabilities 910 911Obtains capability information of the network corresponding to the **netHandle**. This API returns the result synchronously. 912 913**Required permission**: ohos.permission.GET_NETWORK_INFO 914 915**System capability**: SystemCapability.Communication.NetManager.Core 916 917**Parameters** 918 919| Name | Type | Mandatory| Description | 920| --------- | ----------------------- | ---- | ---------------- | 921| netHandle | [NetHandle](#nethandle8) | Yes | Handle of the data network.| 922 923**Return value** 924 925| Type | Description | 926| --------------------------------------------- | --------------------------------- | 927| [NetCapabilities](#netcapabilities8) | Network capability information.| 928 929**Error codes** 930 931| ID| Error Message | 932| ------- | ----------------------------- | 933| 201 | Permission denied. | 934| 401 | Parameter error. | 935| 2100001 | Invalid parameter value. | 936| 2100002 | Operation failed. Cannot connect to service.| 937| 2100003 | System internal error. | 938 939**Example** 940 941```ts 942import connection from '@ohos.net.connection' 943 944let netHandle = connection.getDefaultNetSync(); 945let getNetCapabilitiesSync = connection.getNetCapabilitiesSync(netHandle); 946``` 947 948## connection.isDefaultNetMetered<sup>9+</sup> 949 950isDefaultNetMetered(callback: AsyncCallback\<boolean>): void 951 952Checks whether the data traffic usage on the current network is metered. This API uses an asynchronous callback to return the result. 953 954**Required permission**: ohos.permission.GET_NETWORK_INFO 955 956**System capability**: SystemCapability.Communication.NetManager.Core 957 958**Parameters** 959 960| Name | Type | Mandatory| Description | 961| -------- | ----------------------- | ---- | -------------------------------------- | 962| callback | AsyncCallback\<boolean> | Yes | Callback used to return the result. The value **true** indicates the data traffic usage is metered.| 963 964**Error codes** 965 966| ID| Error Message | 967| ------- | ----------------------------- | 968| 201 | Permission denied. | 969| 401 | Parameter error. | 970| 2100002 | Operation failed. Cannot connect to service.| 971| 2100003 | System internal error. | 972 973**Example** 974 975```ts 976import connection from '@ohos.net.connection' 977import { BusinessError } from '@ohos.base' 978 979connection.isDefaultNetMetered((error: BusinessError, data: boolean) => { 980 console.log(JSON.stringify(error)) 981 console.log('data: ' + data) 982}) 983``` 984 985## connection.isDefaultNetMetered<sup>9+</sup> 986 987isDefaultNetMetered(): Promise\<boolean> 988 989Checks whether the data traffic usage on the current network is metered. This API uses a promise to return the result. 990 991**Required permission**: ohos.permission.GET_NETWORK_INFO 992 993**System capability**: SystemCapability.Communication.NetManager.Core 994 995**Return value** 996 997| Type | Description | 998| ----------------- | ----------------------------------------------- | 999| Promise\<boolean> | Promise used to return the result. The value **true** indicates the data traffic usage is metered.| 1000 1001**Error codes** 1002 1003| ID| Error Message | 1004| ------- | ----------------------------- | 1005| 201 | Permission denied. | 1006| 401 | Parameter error. | 1007| 2100002 | Operation failed. Cannot connect to service.| 1008| 2100003 | System internal error. | 1009 1010**Example** 1011 1012```ts 1013import connection from '@ohos.net.connection' 1014 1015connection.isDefaultNetMetered().then((data: boolean) => { 1016 console.log('data: ' + data) 1017}) 1018``` 1019 1020## connection.isDefaultNetMeteredSync<sup>10+</sup> 1021 1022isDefaultNetMeteredSync(): boolean 1023 1024Checks whether the data traffic usage on the current network is metered. This API returns the result synchronously. 1025 1026**Required permission**: ohos.permission.GET_NETWORK_INFO 1027 1028**System capability**: SystemCapability.Communication.NetManager.Core 1029 1030**Return value** 1031 1032| Type | Description | 1033| ----------------- | ----------------------------------------------- | 1034| boolean | The value **true** indicates the data traffic usage is metered.| 1035 1036**Error codes** 1037 1038| ID| Error Message | 1039| ------- | ----------------------------- | 1040| 201 | Permission denied. | 1041| 401 | Parameter error. | 1042| 2100002 | Operation failed. Cannot connect to service.| 1043| 2100003 | System internal error. | 1044 1045**Example** 1046 1047```ts 1048import connection from '@ohos.net.connection' 1049 1050let isMetered = connection.isDefaultNetMeteredSync(); 1051``` 1052 1053## connection.hasDefaultNet<sup>8+</sup> 1054 1055hasDefaultNet(callback: AsyncCallback\<boolean>): void 1056 1057Checks whether the default data network is activated. This API uses an asynchronous callback to return the result. You can use [getDefaultNet](#connectiongetdefaultnet) to obtain the default data network, if any. 1058 1059**Required permission**: ohos.permission.GET_NETWORK_INFO 1060 1061**System capability**: SystemCapability.Communication.NetManager.Core 1062 1063**Parameters** 1064 1065| Name | Type | Mandatory| Description | 1066| -------- | ----------------------- | ---- | -------------------------------------- | 1067| callback | AsyncCallback\<boolean> | Yes | Callback used to return the result. The value **true** indicates the default data network is activated.| 1068 1069**Error codes** 1070 1071| ID| Error Message | 1072| ------- | ----------------------------- | 1073| 201 | Permission denied. | 1074| 401 | Parameter error. | 1075| 2100002 | Operation failed. Cannot connect to service.| 1076| 2100003 | System internal error. | 1077 1078**Example** 1079 1080```ts 1081import connection from '@ohos.net.connection' 1082import { BusinessError } from '@ohos.base' 1083 1084connection.hasDefaultNet((error: BusinessError, data: boolean) => { 1085 console.log(JSON.stringify(error)) 1086 console.log('data: ' + data) 1087}) 1088``` 1089 1090## connection.hasDefaultNet<sup>8+</sup> 1091 1092hasDefaultNet(): Promise\<boolean> 1093 1094Checks whether the default data network is activated. This API uses a promise to return the result. You can use [getDefaultNet](#connectiongetdefaultnet) to obtain the default data network, if any. 1095 1096**Required permission**: ohos.permission.GET_NETWORK_INFO 1097 1098**System capability**: SystemCapability.Communication.NetManager.Core 1099 1100**Return value** 1101 1102| Type | Description | 1103| ----------------- | ----------------------------------------------- | 1104| Promise\<boolean> | Promise used to return the result. The value **true** indicates that the default data network is activated.| 1105 1106**Error codes** 1107 1108| ID| Error Message | 1109| ------- | ----------------------------- | 1110| 201 | Permission denied. | 1111| 401 | Parameter error. | 1112| 2100002 | Operation failed. Cannot connect to service.| 1113| 2100003 | System internal error. | 1114 1115**Example** 1116 1117```ts 1118import connection from '@ohos.net.connection' 1119connection.hasDefaultNet().then((data: boolean) => { 1120 console.log('data: ' + data) 1121}) 1122``` 1123 1124## connection.hasDefaultNetSync<sup>10+</sup> 1125 1126hasDefaultNetSync(): boolean 1127 1128Checks whether the default data network is activated. This API returns the result synchronously. 1129 1130**Required permission**: ohos.permission.GET_NETWORK_INFO 1131 1132**System capability**: SystemCapability.Communication.NetManager.Core 1133 1134**Return value** 1135 1136| Type | Description | 1137| ----------------- | ----------------------------------------------- | 1138| boolean | The value **true** indicates the default data network is activated.| 1139 1140**Error codes** 1141 1142| ID| Error Message | 1143| ------- | ----------------------------- | 1144| 201 | Permission denied. | 1145| 401 | Parameter error. | 1146| 2100002 | Operation failed. Cannot connect to service.| 1147| 2100003 | System internal error. | 1148 1149**Example** 1150 1151```ts 1152import connection from '@ohos.net.connection' 1153 1154let isDefaultNet = connection.hasDefaultNetSync(); 1155``` 1156 1157## connection.enableAirplaneMode<sup>8+</sup> 1158 1159enableAirplaneMode(callback: AsyncCallback\<void>): void 1160 1161Enables the airplane mode. This API uses an asynchronous callback to return the result. 1162 1163**System API**: This is a system API. 1164 1165**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 1166 1167**System capability**: SystemCapability.Communication.NetManager.Core 1168 1169**Parameters** 1170 1171| Name | Type | Mandatory| Description | 1172| -------- | ------------------------------------------------- | ---- | ------------------ | 1173| callback | AsyncCallback\<void> | Yes | Callback used to return the result. | 1174 1175**Error codes** 1176 1177| ID| Error Message | 1178| ------- | ----------------------------- | 1179| 201 | Permission denied. | 1180| 202 | Non-system applications use system APIs. | 1181| 401 | Parameter error. | 1182| 2100002 | Operation failed. Cannot connect to service.| 1183| 2100003 | System internal error. | 1184 1185**Example** 1186 1187```ts 1188import connection from '@ohos.net.connection' 1189import { BusinessError } from '@ohos.base' 1190 1191connection.enableAirplaneMode((error: BusinessError) => { 1192 console.log(JSON.stringify(error)) 1193}) 1194``` 1195 1196## connection.enableAirplaneMode<sup>8+</sup> 1197 1198enableAirplaneMode(): Promise\<void> 1199 1200Enables the airplane mode. This API uses a promise to return the result. 1201 1202**System API**: This is a system API. 1203 1204**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 1205 1206**System capability**: SystemCapability.Communication.NetManager.Core 1207 1208**Return value** 1209 1210| Type | Description | 1211| ------------------------------------------- | ----------------------------- | 1212| Promise\<void> | Promise that returns no value.| 1213 1214**Error codes** 1215 1216| ID| Error Message | 1217| ------- | ----------------------------- | 1218| 201 | Permission denied. | 1219| 202 | Non-system applications use system APIs. | 1220| 401 | Parameter error. | 1221| 2100002 | Operation failed. Cannot connect to service.| 1222| 2100003 | System internal error. | 1223 1224**Example** 1225 1226```ts 1227import connection from '@ohos.net.connection' 1228 1229connection.enableAirplaneMode().then((error: void) => { 1230 console.log(JSON.stringify(error)) 1231}) 1232``` 1233 1234## connection.disableAirplaneMode<sup>8+</sup> 1235 1236disableAirplaneMode(callback: AsyncCallback\<void>): void 1237 1238Disables the airplane mode. This API uses an asynchronous callback to return the result. 1239 1240**System API**: This is a system API. 1241 1242**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 1243 1244**System capability**: SystemCapability.Communication.NetManager.Core 1245 1246**Parameters** 1247 1248| Name | Type | Mandatory| Description | 1249| -------- | ------------------------------------------------- | ---- | ------------------ | 1250| 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.| 1251 1252**Error codes** 1253 1254| ID| Error Message | 1255| ------- | ----------------------------- | 1256| 201 | Permission denied. | 1257| 202 | Non-system applications use system APIs. | 1258| 401 | Parameter error. | 1259| 2100002 | Operation failed. Cannot connect to service.| 1260| 2100003 | System internal error. | 1261 1262**Example** 1263 1264```ts 1265import connection from '@ohos.net.connection' 1266import { BusinessError } from '@ohos.base' 1267 1268connection.disableAirplaneMode((error: BusinessError) => { 1269 console.log(JSON.stringify(error)) 1270}) 1271``` 1272 1273## connection.disableAirplaneMode<sup>8+</sup> 1274 1275disableAirplaneMode(): Promise\<void> 1276 1277Disables the airplane mode. This API uses a promise to return the result. 1278 1279**System API**: This is a system API. 1280 1281**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL 1282 1283**System capability**: SystemCapability.Communication.NetManager.Core 1284 1285**Return value** 1286 1287| Type | Description | 1288| ------------------------------------------- | ----------------------------- | 1289| Promise\<void> | Promise that returns no value.| 1290 1291**Error codes** 1292 1293| ID| Error Message | 1294| ------- | ----------------------------- | 1295| 201 | Permission denied. | 1296| 202 | Non-system applications use system APIs. | 1297| 401 | Parameter error. | 1298| 2100002 | Operation failed. Cannot connect to service.| 1299| 2100003 | System internal error. | 1300 1301**Example** 1302 1303```ts 1304import connection from '@ohos.net.connection' 1305 1306connection.disableAirplaneMode().then((error: void) => { 1307 console.log(JSON.stringify(error)) 1308}) 1309``` 1310 1311## connection.reportNetConnected<sup>8+</sup> 1312 1313reportNetConnected(netHandle: NetHandle, callback: AsyncCallback<void>): void 1314 1315Reports connection of the data network to the network management module. This API uses an asynchronous callback to return the result. 1316 1317**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET 1318 1319**System capability**: SystemCapability.Communication.NetManager.Core 1320 1321**Parameters** 1322 1323| Name| Type| Mandatory| Description| 1324| -------- | -------- | -------- | -------- | 1325| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).| 1326| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the network status is reported successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 1327 1328**Error codes** 1329 1330| ID| Error Message | 1331| ------- | ----------------------------- | 1332| 201 | Permission denied. | 1333| 401 | Parameter error. | 1334| 2100001 | Invalid parameter value. | 1335| 2100002 | Operation failed. Cannot connect to service.| 1336| 2100003 | System internal error. | 1337 1338**Example** 1339 1340```ts 1341import connection from '@ohos.net.connection' 1342import { BusinessError } from '@ohos.base' 1343 1344connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 1345 connection.reportNetConnected(netHandle, (error: BusinessError) => { 1346 console.log(JSON.stringify(error)) 1347 }); 1348}); 1349``` 1350 1351## connection.reportNetConnected<sup>8+</sup> 1352 1353reportNetConnected(netHandle: NetHandle): Promise<void> 1354 1355Reports connection of the data network to the network management module. This API uses a promise to return the result. 1356 1357**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET 1358 1359**System capability**: SystemCapability.Communication.NetManager.Core 1360 1361**Parameters** 1362 1363| Name| Type| Mandatory| Description| 1364| -------- | -------- | -------- | -------- | 1365| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).| 1366 1367**Return value** 1368| Type| Description| 1369| -------- | -------- | 1370| Promise<void> | Promise that returns no value.| 1371 1372**Error codes** 1373 1374| ID| Error Message | 1375| ------- | ----------------------------- | 1376| 201 | Permission denied. | 1377| 401 | Parameter error. | 1378| 2100001 | Invalid parameter value. | 1379| 2100002 | Operation failed. Cannot connect to service.| 1380| 2100003 | System internal error. | 1381 1382**Example** 1383 1384```ts 1385import connection from '@ohos.net.connection' 1386connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 1387 connection.reportNetConnected(netHandle).then(() => { 1388 console.log(`report success`) 1389 }); 1390}); 1391``` 1392 1393## connection.reportNetDisconnected<sup>8+</sup> 1394 1395reportNetDisconnected(netHandle: NetHandle, callback: AsyncCallback<void>): void 1396 1397Reports disconnection of the data network to the network management module. This API uses an asynchronous callback to return the result. 1398 1399**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET 1400 1401**System capability**: SystemCapability.Communication.NetManager.Core 1402 1403**Parameters** 1404 1405| Name| Type| Mandatory| Description| 1406| -------- | -------- | -------- | -------- | 1407| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).| 1408| callback | AsyncCallback<void> | Yes| Callback used to return the result. If the network status is reported successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 1409 1410**Error codes** 1411 1412| ID| Error Message | 1413| ------- | ----------------------------- | 1414| 201 | Permission denied. | 1415| 401 | Parameter error. | 1416| 2100001 | Invalid parameter value. | 1417| 2100002 | Operation failed. Cannot connect to service.| 1418| 2100003 | System internal error. | 1419 1420**Example** 1421 1422```ts 1423import connection from '@ohos.net.connection' 1424connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 1425 connection.reportNetDisconnected(netHandle).then( () => { 1426 console.log(`report success`) 1427 }); 1428}); 1429``` 1430 1431## connection.reportNetDisconnected<sup>8+</sup> 1432 1433reportNetDisconnected(netHandle: NetHandle): Promise<void> 1434 1435Reports disconnection of the data network to the network management module. This API uses a promise to return the result. 1436 1437**Permission required**: ohos.permission.GET_NETWORK_INFO and ohos.permission.INTERNET 1438 1439**System capability**: SystemCapability.Communication.NetManager.Core 1440 1441**Parameters** 1442 1443| Name| Type| Mandatory| Description| 1444| -------- | -------- | -------- | -------- | 1445| netHandle | [NetHandle](#nethandle) | Yes| Handle of the data network. For details, see [NetHandle](#nethandle).| 1446 1447**Return value** 1448| Type| Description| 1449| -------- | -------- | 1450| Promise<void> | Promise that returns no value.| 1451 1452**Error codes** 1453 1454| ID| Error Message | 1455| ------- | ----------------------------- | 1456| 201 | Permission denied. | 1457| 401 | Parameter error. | 1458| 2100001 | Invalid parameter value. | 1459| 2100002 | Operation failed. Cannot connect to service.| 1460| 2100003 | System internal error. | 1461 1462**Example** 1463 1464```ts 1465import connection from '@ohos.net.connection' 1466connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 1467 connection.reportNetDisconnected(netHandle).then( () => { 1468 console.log(`report success`) 1469 }); 1470}); 1471``` 1472 1473## connection.getAddressesByName<sup>8+</sup> 1474 1475getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void 1476 1477Resolves the host name by using the default network to obtain all IP addresses. This API uses an asynchronous callback to return the result. 1478 1479**Required permissions**: ohos.permission.INTERNET 1480 1481**System capability**: SystemCapability.Communication.NetManager.Core 1482 1483**Parameters** 1484 1485| Name | Type | Mandatory| Description | 1486| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ | 1487| host | string | Yes | Host name to resolve. | 1488| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes | Callback used to return the result. If all IP addresses are successfully obtained, **error** is **undefined**, and **data** is the list of all obtained IP addresses. Otherwise, **error** is an error object.| 1489 1490**Error codes** 1491 1492| ID| Error Message | 1493| ------- | ----------------------------- | 1494| 201 | Permission denied. | 1495| 401 | Parameter error. | 1496| 2100001 | Invalid parameter value. | 1497| 2100002 | Operation failed. Cannot connect to service.| 1498| 2100003 | System internal error. | 1499 1500**Example** 1501 1502```ts 1503import connection from '@ohos.net.connection' 1504import { BusinessError } from "@ohos.base" 1505connection.getAddressesByName("xxxx", (error: BusinessError, data: connection.NetAddress[]) => { 1506 console.log(JSON.stringify(error)) 1507 console.log(JSON.stringify(data)) 1508}) 1509``` 1510 1511## connection.getAddressesByName<sup>8+</sup> 1512 1513getAddressesByName(host: string): Promise\<Array\<NetAddress>> 1514 1515Resolves the host name by using the default network to obtain all IP addresses. This API uses a promise to return the result. 1516 1517**Required permissions**: ohos.permission.INTERNET 1518 1519**System capability**: SystemCapability.Communication.NetManager.Core 1520 1521**Parameters** 1522 1523| Name| Type | Mandatory| Description | 1524| ------ | ------ | ---- | ------------------ | 1525| host | string | Yes | Host name to resolve.| 1526 1527**Return value** 1528 1529| Type | Description | 1530| ------------------------------------------- | ----------------------------- | 1531| Promise\<Array\<[NetAddress](#netaddress)>> | Promise used to return the result.| 1532 1533**Error codes** 1534 1535| ID| Error Message | 1536| ------- | ----------------------------- | 1537| 201 | Permission denied. | 1538| 401 | Parameter error. | 1539| 2100001 | Invalid parameter value. | 1540| 2100002 | Operation failed. Cannot connect to service.| 1541| 2100003 | System internal error. | 1542 1543**Example** 1544 1545```ts 1546import connection from '@ohos.net.connection' 1547connection.getAddressesByName("xxxx").then((data: connection.NetAddress[]) => { 1548 console.log(JSON.stringify(data)) 1549}) 1550``` 1551 1552## NetConnection 1553 1554Represents the network connection handle. 1555 1556> **NOTE** 1557> When a device changes to the network connected state, the **netAvailable**, **netCapabilitiesChange**, and **netConnectionPropertiesChange** events will be triggered. 1558> When a device changes to the network disconnected state, the **netLost** event will be triggered. 1559> When a device switches from a Wi-Fi network to a cellular network, the **netLost** event will be first triggered to indicate that the Wi-Fi network is lost and then the **netAvaliable** event will be triggered to indicate that the cellular network is available. 1560 1561### register<sup>8+</sup> 1562 1563register(callback: AsyncCallback\<void>): void 1564 1565Registers a listener for network status changes. 1566 1567**Required permission**: ohos.permission.GET_NETWORK_INFO 1568 1569**System capability**: SystemCapability.Communication.NetManager.Core 1570 1571**Parameters** 1572 1573| Name | Type | Mandatory| Description | 1574| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1575| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If a listener for network status changes is registered successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 1576 1577**Error codes** 1578 1579| ID| Error Message | 1580| ------- | ----------------------------- | 1581| 201 | Permission denied. | 1582| 401 | Parameter error. | 1583| 2100002 | Operation failed. Cannot connect to service.| 1584| 2100003 | System internal error. | 1585| 2101008 | The same callback exists. | 1586| 2101022 | The number of requests exceeded the maximum. | 1587 1588**Example** 1589 1590```ts 1591import connection from '@ohos.net.connection' 1592import { BusinessError } from "@ohos.base" 1593let netCon: connection.NetConnection = connection.createNetConnection(); 1594netCon.register((error: BusinessError) => { 1595 console.log(JSON.stringify(error)) 1596}) 1597``` 1598 1599### unregister<sup>8+</sup> 1600 1601unregister(callback: AsyncCallback\<void>): void 1602 1603Unregisters the listener for network status changes. 1604 1605**System capability**: SystemCapability.Communication.NetManager.Core 1606 1607**Parameters** 1608 1609| Name | Type | Mandatory| Description | 1610| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1611| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If a listener for network status changes is unregistered successfully, **error** is **undefined**. Otherwise, **error** is an error object.| 1612 1613**Error codes** 1614 1615| ID| Error Message | 1616| ------- | ----------------------------- | 1617| 201 | Permission denied.| 1618| 401 | Parameter error. | 1619| 2100002 | Operation failed. Cannot connect to service.| 1620| 2100003 | System internal error. | 1621| 2101007 | The callback is not exists. | 1622 1623**Example** 1624 1625```ts 1626import connection from '@ohos.net.connection' 1627import { BusinessError } from "@ohos.base" 1628let netCon: connection.NetConnection = connection.createNetConnection(); 1629netCon.unregister((error: BusinessError) => { 1630 console.log(JSON.stringify(error)) 1631}) 1632``` 1633 1634### on('netAvailable')<sup>8+</sup> 1635 1636on(type: 'netAvailable', callback: Callback\<NetHandle>): void 1637 1638Registers a listener for **netAvailable** events. 1639 1640**Model restriction**: Before you call this API, make sure that you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network. 1641 1642**System capability**: SystemCapability.Communication.NetManager.Core 1643 1644**Parameters** 1645 1646| Name | Type | Mandatory| Description | 1647| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 1648| type | string | Yes | Event type. This field has a fixed value of **netAvailable**.<br>**netAvailable**: event indicating that the data network is available.| 1649| callback | Callback\<[NetHandle](#nethandle)> | Yes | Callback used to return the network handle.| 1650 1651**Example** 1652 1653```ts 1654import connection from '@ohos.net.connection' 1655import { BusinessError } from "@ohos.base" 1656 1657// Create a NetConnection object. 1658let netCon: connection.NetConnection = connection.createNetConnection(); 1659 1660// Call register to register a listener. 1661netCon.register((error: BusinessError) => { 1662 console.log(JSON.stringify(error)) 1663}) 1664 1665// Subscribe to netAvailable events. Event notifications can be received only after register is called. 1666netCon.on('netAvailable', (data: connection.NetHandle) => { 1667 console.log(JSON.stringify(data)) 1668}) 1669 1670// Call unregister to unregister the listener. 1671netCon.unregister((error: BusinessError) => { 1672 console.log(JSON.stringify(error)) 1673}) 1674``` 1675 1676### on('netBlockStatusChange')<sup>8+</sup> 1677 1678on(type: 'netBlockStatusChange', callback: Callback<{ netHandle: NetHandle, blocked: boolean }>): void 1679 1680Registers a listener for **netBlockStatusChange** events. This API uses an asynchronous callback to return the result. 1681 1682**Model restriction**: Before you call this API, make sure that you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network. 1683 1684**System capability**: SystemCapability.Communication.NetManager.Core 1685 1686**Parameters** 1687 1688| Name | Type | Mandatory| Description | 1689| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1690| type | string | Yes | Event type. This field has a fixed value of **netBlockStatusChange**.<br>**netBlockStatusChange**: event indicating a change in the network blocking status.| 1691| callback | Callback<{ netHandle: [NetHandle](#nethandle), blocked: boolean }> | Yes | Callback used to return the network handle (**netHandle**) and network status (**blocked**).| 1692 1693**Example** 1694 1695```ts 1696import connection from '@ohos.net.connection' 1697import { BusinessError } from "@ohos.base" 1698 1699// Create a NetConnection object. 1700let netCon: connection.NetConnection = connection.createNetConnection(); 1701 1702// Call register to register a listener. 1703netCon.register((error: BusinessError) => { 1704 console.log(JSON.stringify(error)) 1705}) 1706 1707// Subscribe to netAvailable events. Event notifications can be received only after register is called. 1708netCon.on('netAvailable', (data: connection.NetHandle) => { 1709 console.log(JSON.stringify(data)) 1710}) 1711 1712// Call unregister to unregister the listener. 1713netCon.unregister((error: BusinessError) => { 1714 console.log(JSON.stringify(error)) 1715}) 1716``` 1717 1718### on('netCapabilitiesChange')<sup>8+</sup> 1719 1720on(type: 'netCapabilitiesChange', callback: Callback\<NetCapabilityInfo>): void 1721 1722Registers a listener for **netCapabilitiesChange** events. 1723 1724**Model restriction**: Before you call this API, make sure that you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network. 1725 1726**System capability**: SystemCapability.Communication.NetManager.Core 1727 1728**Parameters** 1729 1730| Name | Type | Mandatory| Description | 1731| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1732| type | string | Yes | Event type. This field has a fixed value of **netCapabilitiesChange**.<br>**netCapabilitiesChange**: event indicating that the network capabilities have changed.| 1733| callback | Callback<[NetCapabilityInfo](#netcapabilityinfo)> | Yes | Callback used to return the network handle (**netHandle**) and capability information (**netCap**).| 1734 1735**Example** 1736 1737```ts 1738import connection from '@ohos.net.connection' 1739import { BusinessError } from "@ohos.base" 1740 1741// Create a NetConnection object. 1742let netCon: connection.NetConnection = connection.createNetConnection(); 1743 1744// Call register to register a listener. 1745netCon.register((error: BusinessError) => { 1746 console.log(JSON.stringify(error)) 1747}) 1748 1749// Subscribe to netAvailable events. Event notifications can be received only after register is called. 1750netCon.on('netAvailable', (data: connection.NetHandle) => { 1751 console.log(JSON.stringify(data)) 1752}) 1753 1754// Call unregister to unregister the listener. 1755netCon.unregister((error: BusinessError) => { 1756 console.log(JSON.stringify(error)) 1757}) 1758``` 1759 1760### on('netConnectionPropertiesChange')<sup>8+</sup> 1761 1762on(type: 'netConnectionPropertiesChange', callback: Callback<{ netHandle: NetHandle, connectionProperties: 1763ConnectionProperties }>): void 1764 1765Registers a listener for **netConnectionPropertiesChange** events. 1766 1767**Model restriction**: Before you call this API, make sure that you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network. 1768 1769**System capability**: SystemCapability.Communication.NetManager.Core 1770 1771**Parameters** 1772 1773| Name | Type | Mandatory| Description | 1774| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1775| type | string | Yes | Event type. This field has a fixed value of **netConnectionPropertiesChange**.<br>**netConnectionPropertiesChange**: event indicating that network connection properties have changed.| 1776| callback | Callback<{ netHandle: [NetHandle](#nethandle), connectionProperties: [ConnectionProperties](#connectionproperties) }> | Yes | Callback used to return the network handle (**netHandle**) and connection information (**connectionProperties**).| 1777 1778**Example** 1779 1780```ts 1781import connection from '@ohos.net.connection' 1782import { BusinessError } from "@ohos.base" 1783 1784// Create a NetConnection object. 1785let netCon: connection.NetConnection = connection.createNetConnection(); 1786 1787// Call register to register a listener. 1788netCon.register((error: BusinessError) => { 1789 console.log(JSON.stringify(error)) 1790}) 1791 1792// Subscribe to netAvailable events. Event notifications can be received only after register is called. 1793netCon.on('netAvailable', (data: connection.NetHandle) => { 1794 console.log(JSON.stringify(data)) 1795}) 1796 1797// Call unregister to unregister the listener. 1798netCon.unregister((error: BusinessError) => { 1799 console.log(JSON.stringify(error)) 1800}) 1801``` 1802 1803### on('netLost')<sup>8+</sup> 1804 1805on(type: 'netLost', callback: Callback\<NetHandle>): void 1806 1807Registers a listener for **netLost** events. 1808 1809**Model restriction**: Before you call this API, make sure that you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network. 1810 1811**System capability**: SystemCapability.Communication.NetManager.Core 1812 1813**Parameters** 1814 1815| Name | Type | Mandatory| Description | 1816| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 1817| type | string | Yes | Event type. This field has a fixed value of **netLost**.<br>netLost: event indicating that the network is interrupted or normally disconnected.| 1818| callback | Callback\<[NetHandle](#nethandle)> | Yes | Callback used to return the network handle (**netHandle**).| 1819 1820**Example** 1821 1822```ts 1823import connection from '@ohos.net.connection' 1824import { BusinessError } from "@ohos.base" 1825 1826// Create a NetConnection object. 1827let netCon: connection.NetConnection = connection.createNetConnection(); 1828 1829// Call register to register a listener. 1830netCon.register((error: BusinessError) => { 1831 console.log(JSON.stringify(error)) 1832}) 1833 1834// Subscribe to netAvailable events. Event notifications can be received only after register is called. 1835netCon.on('netAvailable', (data: connection.NetHandle) => { 1836 console.log(JSON.stringify(data)) 1837}) 1838 1839// Call unregister to unregister the listener. 1840netCon.unregister((error: BusinessError) => { 1841 console.log(JSON.stringify(error)) 1842}) 1843``` 1844 1845### on('netUnavailable')<sup>8+</sup> 1846 1847on(type: 'netUnavailable', callback: Callback\<void>): void 1848 1849Registers a listener for **netUnavailable** events. 1850 1851**Model restriction**: Before you call this API, make sure that you have called **register** to add a listener and called **unregister** API to unsubscribe from status changes of the default network. 1852 1853**System capability**: SystemCapability.Communication.NetManager.Core 1854 1855**Parameters** 1856 1857| Name | Type | Mandatory| Description | 1858| -------- | --------------- | ---- | ------------------------------------------------------------ | 1859| type | string | Yes | Event type. This field has a fixed value of **netUnavailable**.<br>**netUnavailable**: event indicating that the network is unavailable.| 1860| callback | Callback\<void> | Yes | Callback used to return the result, which is empty.| 1861 1862**Example** 1863 1864```ts 1865import connection from '@ohos.net.connection' 1866import { BusinessError } from "@ohos.base" 1867 1868// Create a NetConnection object. 1869let netCon: connection.NetConnection = connection.createNetConnection(); 1870 1871// Call register to register a listener. 1872netCon.register((error: BusinessError) => { 1873 console.log(JSON.stringify(error)) 1874}) 1875 1876// Subscribe to netAvailable events. Event notifications can be received only after register is called. 1877netCon.on('netAvailable', (data: connection.NetHandle) => { 1878 console.log(JSON.stringify(data)) 1879}) 1880 1881// Call unregister to unregister the listener. 1882netCon.unregister((error: BusinessError) => { 1883 console.log(JSON.stringify(error)) 1884}) 1885``` 1886 1887## NetHandle<sup>8+</sup> 1888 1889Defines the handle of the data network. 1890 1891Before invoking **NetHandle** APIs, call **getNetHandle** to obtain a **NetHandle** object. 1892 1893**System capability**: SystemCapability.Communication.NetManager.Core 1894 1895### Attributes 1896 1897| Name | Type | Mandatory| Description | 1898| ------ | ------ | --- |------------------------- | 1899| netId | number | Yes | Network ID. The value **0** indicates no default network. Any other value must be greater than or equal to 100.| 1900 1901### bindSocket<sup>9+</sup> 1902 1903bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void 1904 1905Binds a **TCPSocket** or **UDPSocket** object to the data network. This API uses an asynchronous callback to return the result. 1906 1907**System capability**: SystemCapability.Communication.NetManager.Core 1908 1909**Parameters** 1910 1911| Name | Type | Mandatory| Description | 1912| ----------- | ------------------------ | ---- | -------------------------------| 1913| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | Yes| **TCPSocket** or **UDPSocket** object.| 1914| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the **TCPSocket** or **UDPSocket** object is successfully bound to the current network, **error** is **undefined**. Otherwise, **error** is an error object.| 1915 1916**Error codes** 1917 1918| ID| Error Message | 1919| ------- | ----------------------------- | 1920| 401 | Parameter error. | 1921| 2100001 | Invalid parameter value. | 1922| 2100002 | Operation failed. Cannot connect to service.| 1923| 2100003 | System internal error. | 1924 1925**Example** 1926 1927```ts 1928import socket from "@ohos.net.socket"; 1929import connection from '@ohos.net.connection'; 1930import { BusinessError } from '@ohos.base'; 1931 1932interface Data { 1933 message: ArrayBuffer, 1934 remoteInfo: socket.SocketRemoteInfo 1935} 1936 1937connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 1938 let tcp = socket.constructTCPSocketInstance(); 1939 let udp = socket.constructUDPSocketInstance(); 1940 let socketType = "TCPSocket"; 1941 if (socketType == "TCPSocket") { 1942 tcp.bind({address:"192.168.xxx.xxx", 1943 port:8080, 1944 family:1} as socket.NetAddress, (error: Error) => { 1945 if (error) { 1946 console.log('bind fail'); 1947 return; 1948 } 1949 netHandle.bindSocket(tcp, (error: BusinessError, data: void) => { 1950 if (error) { 1951 console.log(JSON.stringify(error)); 1952 } else { 1953 console.log(JSON.stringify(data)); 1954 } 1955 }) 1956 }) 1957 } else { 1958 let callback: (value: Data) => void = (value: Data) => { 1959 console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo); 1960 } 1961 udp.bind({address:"192.168.xxx.xxx", 1962 port:8080, 1963 family:1} as socket.NetAddress, (error: BusinessError) => { 1964 if (error) { 1965 console.log('bind fail'); 1966 return; 1967 } 1968 udp.on('message', (data: Data) => { 1969 console.log(JSON.stringify(data)) 1970 }); 1971 netHandle.bindSocket(udp, (error: BusinessError, data: void) => { 1972 if (error) { 1973 console.log(JSON.stringify(error)); 1974 } else { 1975 console.log(JSON.stringify(data)); 1976 } 1977 }) 1978 }) 1979 } 1980}) 1981``` 1982 1983### bindSocket<sup>9+</sup> 1984 1985bindSocket(socketParam: TCPSocket \| UDPSocket): Promise\<void>; 1986 1987Binds a **TCPSocket** or **UDPSocket** object to the data network. This API uses a promise to return the result. 1988 1989**System capability**: SystemCapability.Communication.NetManager.Core 1990 1991**Parameters** 1992 1993| Name | Type | Mandatory | Description | 1994| --------------- | --------------------- | ---- | ------------------------------ | 1995| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | Yes | **TCPSocket** or **UDPSocket** object.| 1996 1997**Return value** 1998 1999| Type | Description | 2000| -------------- | ---------------------- | 2001| Promise\<void> | Promise that returns no value.| 2002 2003**Error codes** 2004 2005| ID| Error Message | 2006| ------- | ----------------------------- | 2007| 401 | Parameter error. | 2008| 2100001 | Invalid parameter value. | 2009| 2100002 | Operation failed. Cannot connect to service.| 2010| 2100003 | System internal error. | 2011 2012**Example** 2013 2014```ts 2015import socket from "@ohos.net.socket"; 2016import connection from '@ohos.net.connection'; 2017import { BusinessError } from '@ohos.base'; 2018interface Data { 2019 message: ArrayBuffer, 2020 remoteInfo: socket.SocketRemoteInfo 2021} 2022 2023connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2024 let tcp = socket.constructTCPSocketInstance(); 2025 let udp = socket.constructUDPSocketInstance(); 2026 let socketType = "TCPSocket"; 2027 if (socketType == "TCPSocket") { 2028 tcp.bind({address:"192.168.xxx.xxx", 2029 port:8080, 2030 family:1} as socket.NetAddress, (error: Error) => { 2031 if (error) { 2032 console.log('bind fail'); 2033 return; 2034 } 2035 netHandle.bindSocket(tcp, (error: BusinessError, data: void) => { 2036 if (error) { 2037 console.log(JSON.stringify(error)); 2038 } else { 2039 console.log(JSON.stringify(data)); 2040 } 2041 }) 2042 }) 2043 } else { 2044 let callback: (value: Data) => void = (value: Data) => { 2045 console.log("on message, message:" + value.message + ", remoteInfo:" + value.remoteInfo); 2046 } 2047 udp.bind({address:"192.168.xxx.xxx", 2048 port:8080, 2049 family:1} as socket.NetAddress, (error: BusinessError) => { 2050 if (error) { 2051 console.log('bind fail'); 2052 return; 2053 } 2054 udp.on('message', (data: Data) => { 2055 console.log(JSON.stringify(data)) 2056 }); 2057 netHandle.bindSocket(udp, (error: BusinessError, data: void) => { 2058 if (error) { 2059 console.log(JSON.stringify(error)); 2060 } else { 2061 console.log(JSON.stringify(data)); 2062 } 2063 }) 2064 }) 2065} 2066}) 2067``` 2068 2069### getAddressesByName<sup>8+</sup> 2070 2071getAddressesByName(host: string, callback: AsyncCallback\<Array\<NetAddress>>): void 2072 2073Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses an asynchronous callback to return the result. 2074 2075**Required permissions**: ohos.permission.INTERNET 2076 2077**System capability**: SystemCapability.Communication.NetManager.Core 2078 2079**Parameters** 2080 2081| Name | Type | Mandatory| Description | 2082| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ | 2083| host | string | Yes | Host name to resolve. | 2084| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | Yes | Callback used to return the result. If all IP addresses are successfully obtained, **error** is **undefined**, and **data** is the list of all obtained IP addresses. Otherwise, **error** is an error object.| 2085 2086**Error codes** 2087 2088| ID| Error Message | 2089| ------- | ----------------------------- | 2090| 201 | Permission denied. | 2091| 401 | Parameter error. | 2092| 2100001 | Invalid parameter value. | 2093| 2100002 | Operation failed. Cannot connect to service.| 2094| 2100003 | System internal error. | 2095 2096**Example** 2097 2098```ts 2099import connection from '@ohos.net.connection' 2100import { BusinessError } from "@ohos.base" 2101 2102connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2103 let host = "xxxx"; 2104 netHandle.getAddressesByName(host, (error: BusinessError, data: connection.NetAddress[]) => { 2105 console.log(JSON.stringify(error)) 2106 console.log(JSON.stringify(data)) 2107 }) 2108}) 2109``` 2110 2111### getAddressesByName<sup>8+</sup> 2112 2113getAddressesByName(host: string): Promise\<Array\<NetAddress>> 2114 2115Resolves the host name by using the corresponding network to obtain all IP addresses. This API uses a promise to return the result. 2116 2117**Required permissions**: ohos.permission.INTERNET 2118 2119**System capability**: SystemCapability.Communication.NetManager.Core 2120 2121**Parameters** 2122 2123| Name| Type | Mandatory| Description | 2124| ------ | ------ | ---- | ------------------ | 2125| host | string | Yes | Host name to resolve.| 2126 2127**Return value** 2128 2129| Type | Description | 2130| ------------------------------------------- | ----------------------------- | 2131| Promise\<Array\<[NetAddress](#netaddress)>> | Promise used to return the result.| 2132 2133**Error codes** 2134 2135| ID| Error Message | 2136| ------- | ----------------------------- | 2137| 201 | Permission denied. | 2138| 401 | Parameter error. | 2139| 2100001 | Invalid parameter value. | 2140| 2100002 | Operation failed. Cannot connect to service.| 2141| 2100003 | System internal error. | 2142 2143**Example** 2144 2145```ts 2146import connection from '@ohos.net.connection' 2147 2148connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2149 let host = "xxxx"; 2150 netHandle.getAddressesByName(host).then((data: connection.NetAddress[]) => { 2151 console.log(JSON.stringify(data)) 2152 }) 2153}) 2154``` 2155 2156### getAddressByName<sup>8+</sup> 2157 2158getAddressByName(host: string, callback: AsyncCallback\<NetAddress>): void 2159 2160Resolves the host name by using the corresponding network to obtain the first IP address. This API uses an asynchronous callback to return the result. 2161 2162**Required permissions**: ohos.permission.INTERNET 2163 2164**System capability**: SystemCapability.Communication.NetManager.Core 2165 2166**Parameters** 2167 2168| Name | Type | Mandatory| Description | 2169| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 2170| host | string | Yes | Host name to resolve. | 2171| callback | AsyncCallback\<[NetAddress](#netaddress)> | Yes | Callback used to return the result. If the first IP address is obtained successfully, **error** is **undefined**, and **data** is the first obtained IP address. Otherwise, **error** is an error object.| 2172 2173**Error codes** 2174 2175| ID| Error Message | 2176| ------- | ----------------------------- | 2177| 201 | Permission denied. | 2178| 401 | Parameter error. | 2179| 2100001 | Invalid parameter value. | 2180| 2100002 | Operation failed. Cannot connect to service.| 2181| 2100003 | System internal error. | 2182 2183**Example** 2184 2185```ts 2186import connection from '@ohos.net.connection' 2187import { BusinessError } from "@ohos.base" 2188 2189connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2190 let host = "xxxx"; 2191 netHandle.getAddressByName(host, (error: BusinessError, data: connection.NetAddress) => { 2192 console.log(JSON.stringify(error)) 2193 console.log(JSON.stringify(data)) 2194 }) 2195}) 2196``` 2197 2198### getAddressByName<sup>8+</sup> 2199 2200getAddressByName(host: string): Promise\<NetAddress> 2201 2202Resolves the host name by using the corresponding network to obtain the first IP address. This API uses a promise to return the result. 2203 2204**Required permissions**: ohos.permission.INTERNET 2205 2206**System capability**: SystemCapability.Communication.NetManager.Core 2207 2208**Parameters** 2209 2210| Name| Type | Mandatory| Description | 2211| ------ | ------ | ---- | ------------------ | 2212| host | string | Yes | Host name to resolve.| 2213 2214**Return value** 2215 2216| Type | Description | 2217| ----------------------------------- | ------------------------------- | 2218| Promise\<[NetAddress](#netaddress)> | Promise used to return the result.| 2219 2220**Error codes** 2221 2222| ID| Error Message | 2223| ------- | ----------------------------- | 2224| 201 | Permission denied. | 2225| 401 | Parameter error. | 2226| 2100001 | Invalid parameter value. | 2227| 2100002 | Operation failed. Cannot connect to service.| 2228| 2100003 | System internal error. | 2229 2230**Example** 2231 2232```ts 2233import connection from '@ohos.net.connection' 2234 2235connection.getDefaultNet().then((netHandle: connection.NetHandle) => { 2236 let host = "xxxx"; 2237 netHandle.getAddressByName(host).then((data: connection.NetAddress) => { 2238 console.log(JSON.stringify(data)) 2239 }) 2240}) 2241``` 2242 2243## NetCap<sup>8+</sup> 2244 2245Defines the network capability. 2246 2247**System capability**: SystemCapability.Communication.NetManager.Core 2248 2249| Name | Value | Description | 2250| ------------------------ | ---- | ---------------------- | 2251| NET_CAPABILITY_MMS | 0 | The network can connect to the carrier's Multimedia Messaging Service Center (MMSC) to send and receive multimedia messages.| 2252| NET_CAPABILITY_NOT_METERED | 11 | The network traffic is not metered.| 2253| NET_CAPABILITY_INTERNET | 12 | The network has the Internet access capability, which is set by the network provider.| 2254| NET_CAPABILITY_NOT_VPN | 15 | The network does not use a virtual private network (VPN).| 2255| NET_CAPABILITY_VALIDATED | 16 | The Internet access capability of the network is successfully verified by the connection management module.| 2256 2257## NetBearType<sup>8+</sup> 2258 2259Enumerates network types. 2260 2261**System capability**: SystemCapability.Communication.NetManager.Core 2262 2263| Name | Value | Description | 2264| --------------- | ---- | ----------- | 2265| BEARER_CELLULAR | 0 | Cellular network. | 2266| BEARER_WIFI | 1 | Wi-Fi network.| 2267| BEARER_ETHERNET | 3 | Ethernet network.| 2268 2269## HttpProxy<sup>10+</sup> 2270 2271Represents the HTTP proxy configuration. 2272 2273**System capability**: SystemCapability.Communication.NetManager.Core 2274 2275| Name | Type | Mandatory| Description | 2276| ------ | ------ | --- |------------------------- | 2277| host | string | No | Host name of the proxy server.| 2278| port | number | No | Host port.| 2279| exclusionList | Array<string> | No | List of the names of hosts that do not use a proxy. Host names can be domain names, IP addresses, or wildcards. The detailed matching rules are as follows:<br>- Domain name matching:<br> - Exact match: The host name of the proxy server exactly matches any host name in the list.<br> - Partial match: The host name of the proxy server contains any host name in the list.<br>For example, if **ample.com** is set in the host name list, **ample.com**, **www.ample.com**, and **ample.com:80** are matched, and **www.example.com** and **ample.com.org** are not matched.<br>- IP address matching: The host name of the proxy server exactly matches any IP address in the list.<br>- Both the domain name and IP address are added to the list for matching.<br>- A single asterisk (*) is the only valid wildcard. If the list contains only wildcards, the wildcards match all host names; that is, the HTTP proxy is disabled. A wildcard can only be added independently. It cannot be added to the list together with other domain names or IP addresses. Otherwise, the wildcard does not take effect.<br>- Host names are case insensitive.<br>- Protocol prefixes such as **http** and **https** are ignored during matching.| 2280 2281## NetSpecifier<sup>8+</sup> 2282 2283Provides an instance that bears data network capabilities. 2284 2285**System capability**: SystemCapability.Communication.NetManager.Core 2286 2287| Name | Type | Mandatory | Description | 2288| ----------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 2289| netCapabilities | [NetCapabilities](#netcapabilities) | Yes | Network transmission capabilities and bearer types of the data network. | 2290| bearerPrivateIdentifier | string | No | Network identifier. The identifier of a Wi-Fi network is **wifi**, and that of a cellular network is **slot0** (corresponding to SIM card 1).| 2291 2292## NetCapabilityInfo<sup>10+</sup> 2293 2294Provides an instance that bears data network capabilities. 2295 2296**System capability**: SystemCapability.Communication.NetManager.Core 2297 2298| Name | Type | Mandatory | Description | 2299| ----------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 2300| netHandle | [NetHandle](#nethandle) | Yes | Handle of the data network. | 2301| netCap | [NetCapabilities](#netcapabilities) | No | Network transmission capabilities and bearer types of the data network.| 2302 2303## NetCapabilities<sup>8+</sup> 2304 2305Defines the network capability set. 2306 2307**System capability**: SystemCapability.Communication.NetManager.Core 2308 2309| Name | Type | Mandatory| Description | 2310| --------------------- | ---------------------------------- | --- | ------------------------ | 2311| linkUpBandwidthKbps | number | No| Uplink (device-to-network) bandwidth. The value **0** indicates that the current network bandwidth cannot be evaluated. | 2312| linkDownBandwidthKbps | number | No| Downlink (network-to-device) bandwidth. The value **0** indicates that the current network bandwidth cannot be evaluated. | 2313| networkCap | Array\<[NetCap](#netcap)> | No| Network capability. | 2314| bearerTypes | Array\<[NetBearType](#netbeartype)> | Yes| Network type. | 2315 2316## ConnectionProperties<sup>8+</sup> 2317 2318Defines the network connection properties. 2319 2320**System capability**: SystemCapability.Communication.NetManager.Core 2321 2322| Name | Type | Mandatory| Description | 2323| ------------- | ---------------------------------- | ----|---------------- | 2324| interfaceName | string | Yes|Network interface card (NIC) name. | 2325| domains | string | Yes|Domain. The default value is **""**.| 2326| linkAddresses | Array\<[LinkAddress](#linkaddress)> | Yes|Link information. | 2327| routes | Array\<[RouteInfo](#routeinfo)> | Yes|Route information. | 2328| dnses | Array\<[NetAddress](#netaddress)> | Yes|Network address. For details, see [NetAddress](#netaddress).| 2329| mtu | number | Yes|Maximum transmission unit (MTU). | 2330 2331## RouteInfo<sup>8+</sup> 2332 2333Defines network route information. 2334 2335**System capability**: SystemCapability.Communication.NetManager.Core 2336 2337| Name | Type | Mandatory|Description | 2338| -------------- | --------------------------- | --- |---------------- | 2339| interface | string | Yes|NIC name. | 2340| destination | [LinkAddress](#linkaddress) | Yes|Destination address. | 2341| gateway | [NetAddress](#netaddress) | Yes|Gateway address. | 2342| hasGateway | boolean | Yes|Whether a gateway is present. | 2343| isDefaultRoute | boolean | Yes|Whether the route is the default route.| 2344 2345## LinkAddress<sup>8+</sup> 2346 2347Defines network link information. 2348 2349**System capability**: SystemCapability.Communication.NetManager.Core 2350 2351| Name | Type | Mandatory|Description | 2352| ------------ | ----------------------- |---- |-------------------- | 2353| address | [NetAddress](#netaddress) | Yes| Link address. | 2354| prefixLength | number | Yes|Length of the link address prefix.| 2355 2356## NetAddress<sup>8+</sup> 2357 2358Defines a network address. 2359 2360**System capability**: SystemCapability.Communication.NetManager.Core 2361 2362| Name| Type| Mandatory| Description| 2363| ------- | ------ | -- |------------------------------ | 2364| address | string | Yes|Network address.| 2365| family | number | No|Address family identifier. The value is **1** for IPv4 and **2** for IPv6. The default value is **1**.| 2366| port | number | No|Port number. The value ranges from **0** to **65535**.| 2367