1# @ohos.net.connection (网络连接管理) 2 3网络连接管理提供管理网络一些基础能力,包括获取默认激活的数据网络、获取所有激活数据网络列表、开启关闭飞行模式、获取网络能力信息等功能。 4 5> **说明:** 6> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 7 8## 导入模块 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 18返回一个NetConnection对象,netSpecifier指定关注的网络的各项特征;timeout是超时时间(单位是毫秒);netSpecifier是timeout的必要条件,两者都没有则表示关注默认网络。 19 20**系统能力**:SystemCapability.Communication.NetManager.Core 21 22**参数:** 23 24| 参数名 | 类型 | 必填 | 说明 | 25| ------------ | ----------------------------- | ---- | ------------------------------------------------------------ | 26| netSpecifier | [NetSpecifier](#netspecifier) | 否 | 指定网络的各项特征,不指定或为undefined时关注默认网络。 | 27| timeout | number | 否 | 获取netSpecifier指定的网络时的超时时间,仅netSpecifier存在时生效,undefined时默认值为0。 | 28 29**返回值:** 30 31| 类型 | 说明 | 32| ------------------------------- | -------------------- | 33| [NetConnection](#netconnection) | 所关注的网络的句柄。 | 34 35**示例:** 36 37```ts 38import connection from '@ohos.net.connection' 39 40// 关注默认网络, 不需要传参 41let netConnection = connection.createNetConnection() 42 43// 关注蜂窝网络,需要传入相关网络特征,timeout参数未传入说明未使用超时时间,此时timeout为0 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 55获取默认激活的数据网络,使用callback方式作为异步方法。可以使用[getNetCapabilities](#connectiongetnetcapabilities)去获取网络的类型、拥有的能力等信息。 56 57**需要权限**:ohos.permission.GET_NETWORK_INFO 58 59**系统能力**:SystemCapability.Communication.NetManager.Core 60 61**参数:** 62 63| 参数名 | 类型 | 必填 | 说明 | 64| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 65| callback | AsyncCallback\<[NetHandle](#nethandle)> | 是 | 回调函数。当成功获取默认激活的数据网络时,error为undefined,data为默认激活的数据网络;否则为错误对象 | 66 67**错误码:** 68 69| 错误码ID | 错误信息 | 70| ------- | ----------------------------- | 71| 201 | Permission denied. | 72| 401 | Parameter error. | 73| 2100002 | Operation failed. Cannot connect to service.| 74| 2100003 | System internal error. | 75 76**示例:** 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 92获取默认激活的数据网络,使用Promise方式作为异步方法。可以使用[getNetCapabilities](#connectiongetnetcapabilities)去获取网络的类型、拥有的能力等信息。 93 94**需要权限**:ohos.permission.GET_NETWORK_INFO 95 96**系统能力**:SystemCapability.Communication.NetManager.Core 97 98**返回值:** 99 100| 类型 | 说明 | 101| --------------------------------- | ------------------------------------- | 102| Promise\<[NetHandle](#nethandle)> | 以Promise形式返回默认激活的数据网络。 | 103 104**错误码:** 105 106| 错误码ID | 错误信息 | 107| ------- | ----------------------------- | 108| 201 | Permission denied. | 109| 401 | Parameter error. | 110| 2100002 | Operation failed. Cannot connect to service.| 111| 2100003 | System internal error. | 112 113**示例:** 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 126使用同步方法获取默认激活的数据网络。可以使用[getNetCapabilities](#connectiongetnetcapabilities)去获取网络的类型、拥有的能力等信息。 127 128**需要权限**:ohos.permission.GET_NETWORK_INFO 129 130**系统能力**:SystemCapability.Communication.NetManager.Core 131 132**返回值:** 133 134| 类型 | 说明 | 135| --------- | ---------------------------------- | 136| NetHandle | 以同步方式返回默认激活的数据网络。 | 137 138**错误码:** 139 140| 错误码ID | 错误信息 | 141| ------- | ----------------------------- | 142| 201 | Permission denied. | 143| 401 | Parameter error. | 144| 2100002 | Operation failed. Cannot connect to service.| 145| 2100003 | System internal error. | 146 147**示例:** 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 159获取网络的全局代理配置信息,使用callback方式作为异步方法。 160 161**系统接口**:此接口为系统接口。 162 163**系统能力**:SystemCapability.Communication.NetManager.Core 164 165**参数:** 166 167| 参数名 | 类型 | 必填 | 说明 | 168| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 169| callback | AsyncCallback\<[HttpProxy](#httpproxy10)> | 是 | 回调函数。当成功获取网络的全局代理配置信息时,error为undefined,data为网络的全局代理配置信息;否则为错误对象 | 170 171**错误码:** 172 173| 错误码ID | 错误信息 | 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**示例:** 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 196获取网络的全局代理配置信息,使用Promise方式作为异步方法。 197 198**系统接口**:此接口为系统接口。 199 200**系统能力**:SystemCapability.Communication.NetManager.Core 201 202**返回值:** 203 204| 类型 | 说明 | 205| --------------------------------- | ------------------------------------- | 206| Promise\<[HttpProxy](#httpproxy10)> | 以Promise形式返回网络的全局代理配置信息。 | 207 208**错误码:** 209 210| 错误码ID | 错误信息 | 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**示例:** 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 234设置网络全局Http代理配置信息,使用callback方式作为异步方法。 235 236**系统接口**:此接口为系统接口。 237 238**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 239 240**系统能力**:SystemCapability.Communication.NetManager.Core 241 242**参数:** 243 244| 参数名 | 类型 | 必填 | 说明 | 245| --------- | ----------------------- | ---- | ------------------------------------------------------------ | 246| httpProxy | [HttpProxy](#httpproxy10) | 是 | 网络全局Http代理配置信息 | 247| callback | AsyncCallback\<void> | 是 | 回调函数。当成功设置网络全局Http代理配置信息时,error为undefined,否则为错误对象 | 248 249**错误码:** 250 251| 错误码ID | 错误信息 | 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**示例:** 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 283设置网络全局Http代理配置信息,使用Promise方式作为异步方法。 284 285**系统接口**:此接口为系统接口。 286 287**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 288 289**系统能力**:SystemCapability.Communication.NetManager.Core 290 291**参数:** 292 293| 参数名 | 类型 | 必填 | 说明 | 294| --------- | ------------------------------------------------------------ | ---- | ---------------- | 295| httpProxy | [HttpProxy](#httpproxy10) | 是 | 网络全局Http代理配置信息。 | 296 297**返回值:** 298 299| 类型 | 说明 | 300| ------------------------------------------- | ----------------------------- | 301| Promise\<void> | 无返回值的Promise对象。 | 302 303**错误码:** 304 305| 错误码ID | 错误信息 | 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**示例:** 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 337获取网络默认的代理配置信息。 338如果设置了全局代理,则会返回全局代理配置信息。如果进程使用[setAppNet](#connectionsetappnet)绑定到指定[NetHandle](#nethandle)对应的网络,则返回[NetHandle](#nethandle)对应网络的代理配置信息。在其它情况下,将返回默认网络的代理配置信息。 339使用callback方式作为异步方法。 340 341**系统能力**:SystemCapability.Communication.NetManager.Core 342 343**参数:** 344 345| 参数名 | 类型 | 必填 | 说明 | 346| -------- | -------------------------------------- | ---- | ------------------------------------------------------------ | 347| callback | AsyncCallback<[HttpProxy](#httpproxy10)> | 是 | 回调函数。当成功获取网络默认的代理配置信息时,error为undefined,data为网络默认的代理配置信息;否则为错误对象 | 348 349**错误码:** 350 351| 错误码ID | 错误信息 | 352| -------- | -------------------------------------------- | 353| 2100002 | Operation failed. Cannot connect to service. | 354| 2100003 | System internal error. | 355 356**示例:** 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 372获取网络默认的代理配置信息。 373如果设置了全局代理,则会返回全局代理配置信息。如果进程使用[setAppNet](#connectionsetappnet)绑定到指定[NetHandle](#nethandle)对应的网络,则返回[NetHandle](#nethandle)对应网络的代理配置信息。在其它情况下,将返回默认网络的代理配置信息。 374使用Promise方式作为异步方法。 375 376**系统能力**:SystemCapability.Communication.NetManager.Core 377 378**返回值:** 379 380| 类型 | 说明 | 381| -------------------------------- | ----------------------------------------- | 382| Promise<[HttpProxy](#httpproxy10)> | 以Promise形式返回网络默认的代理配置信息。 | 383 384**错误码:** 385 386| 错误码ID | 错误信息 | 387| -------- | -------------------------------------------- | 388| 2100002 | Operation failed. Cannot connect to service. | 389| 2100003 | System internal error. | 390 391**示例:** 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 408获取App绑定的网络信息,使用callback方式作为异步方法。 409 410**系统能力**:SystemCapability.Communication.NetManager.Core 411 412**参数:** 413 414| 参数名 | 类型 | 必填 | 说明 | 415| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 416| callback | AsyncCallback\<[NetHandle](#nethandle)> | 是 | 回调函数。当成功获取App绑定的网络信息时,error为undefined,data为获取到App绑定的网络信息;否则为错误对象 | 417 418**错误码:** 419 420| 错误码ID | 错误信息 | 421| ------- | ----------------------------- | 422| 401 | Parameter error.| 423| 2100002 | Operation failed. Cannot connect to service.| 424| 2100003 | System internal error. | 425 426**示例:** 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 442获取App绑定的网络信息,使用Promise方式作为异步方法。 443 444**系统能力**:SystemCapability.Communication.NetManager.Core 445 446**返回值:** 447 448| 类型 | 说明 | 449| --------------------------------- | ------------------------------------- | 450| Promise\<[NetHandle](#nethandle)> | 以Promise形式返回App绑定的网络信息。 | 451 452**错误码:** 453 454| 错误码ID | 错误信息 | 455| ------- | ----------------------------- | 456| 401 | Parameter error.| 457| 2100002 | Operation failed. Cannot connect to service.| 458| 2100003 | System internal error. | 459 460**示例:** 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 477使用同步方法获取App绑定的网络信息。 478 479**系统能力**:SystemCapability.Communication.NetManager.Core 480 481**返回值:** 482 483| 类型 | 说明 | 484| --------- | ---------------------------------- | 485| [NetHandle](#nethandle8) | 返回APP绑定的数据网络。 | 486 487**错误码:** 488 489| 错误码ID | 错误信息 | 490| ------- | ----------------------------- | 491| 401 | Parameter error. | 492| 2100002 | Operation failed. Cannot connect to service.| 493| 2100003 | System internal error. | 494 495**示例:** 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 507绑定App到指定网络,绑定后的App只能通过指定网络访问外网,使用callback方式作为异步方法。 508 509**需要权限**:ohos.permission.INTERNET 510 511**系统能力**:SystemCapability.Communication.NetManager.Core 512 513**参数:** 514 515| 参数名 | 类型 | 必填 | 说明 | 516| --------- | ----------------------- | ---- | ------------------------------------------------------------ | 517| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄。 | 518| callback | AsyncCallback\<void> | 是 | 回调函数。当成功绑定App到指定网络时,error为undefined,否则为错误对象 | 519 520**错误码:** 521 522| 错误码ID | 错误信息 | 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**示例:** 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 548绑定App到指定网络,绑定后的App只能通过指定网络访问外网,使用Promise方式作为异步方法。 549 550**需要权限**:ohos.permission.INTERNET 551 552**系统能力**:SystemCapability.Communication.NetManager.Core 553 554**参数:** 555 556| 参数名 | 类型 | 必填 | 说明 | 557| --------- | ------------------------------------------------------------ | ---- | ---------------- | 558| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄。 | 559 560**返回值:** 561 562| 类型 | 说明 | 563| ------------------------------------------- | ----------------------------- | 564| Promise\<void> | 无返回值的Promise对象。 | 565 566**错误码:** 567 568| 错误码ID | 错误信息 | 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**示例:** 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 595获取所有处于连接状态的网络列表,使用callback方式作为异步方法。 596 597**需要权限**:ohos.permission.GET_NETWORK_INFO 598 599**系统能力**:SystemCapability.Communication.NetManager.Core 600 601**参数:** 602 603| 参数名 | 类型 | 必填 | 说明 | 604| -------- | -------- | -------- | -------- | 605| callback | AsyncCallback<Array<[NetHandle](#nethandle)>> | 是 | 回调函数。当成功获取所有处于连接状态的网络列表时,error为undefined,data为激活的数据网络列表;否则为错误对象 | 606 607**错误码:** 608 609| 错误码ID | 错误信息 | 610| ------- | ----------------------------- | 611| 201 | Permission denied. | 612| 401 | Parameter error. | 613| 2100002 | Operation failed. Cannot connect to service.| 614| 2100003 | System internal error. | 615 616**示例:** 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 632获取所有处于连接状态的网络列表,使用Promise方式作为异步方法。 633 634**需要权限**:ohos.permission.GET_NETWORK_INFO 635 636**系统能力**:SystemCapability.Communication.NetManager.Core 637 638**返回值:** 639 640| 类型 | 说明 | 641| -------- | -------- | 642| Promise<Array<[NetHandle](#nethandle)>> | 以Promise形式返回激活的数据网络列表。 | 643 644**错误码:** 645 646| 错误码ID | 错误信息 | 647| ------- | ----------------------------- | 648| 201 | Permission denied. | 649| 401 | Parameter error. | 650| 2100002 | Operation failed. Cannot connect to service.| 651| 2100003 | System internal error. | 652 653**示例:** 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 667使用同步方法获取所有处于连接状态的网络列表。 668 669**需要权限**:ohos.permission.GET_NETWORK_INFO 670 671**系统能力**:SystemCapability.Communication.NetManager.Core 672 673**返回值:** 674 675| 类型 | 说明 | 676| --------- | ---------------------------------- | 677| Array<[NetHandle](#nethandle8)> | 返回激活的数据网络列表。 | 678 679**错误码:** 680 681| 错误码ID | 错误信息 | 682| ------- | ----------------------------- | 683| 201 | Permission denied. | 684| 401 | Parameter error. | 685| 2100002 | Operation failed. Cannot connect to service.| 686| 2100003 | System internal error. | 687 688**示例:** 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 700获取netHandle对应的网络的连接信息,使用callback方式作为异步方法。 701 702**需要权限**:ohos.permission.GET_NETWORK_INFO 703 704**系统能力**:SystemCapability.Communication.NetManager.Core 705 706**参数:** 707 708| 参数名 | 类型 | 必填 | 说明 | 709| --------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 710| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄。 | 711| callback | AsyncCallback\<[ConnectionProperties](#connectionproperties)> | 是 | 回调函数。当成功获取netHandle对应的网络的连接信息时,error为undefined,data为获取的网络连接信息;否则为错误对象 | 712 713**错误码:** 714 715| 错误码ID | 错误信息 | 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**示例:** 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 741获取netHandle对应的网络的连接信息,使用Promise方式作为异步方法。 742 743**需要权限**:ohos.permission.GET_NETWORK_INFO 744 745**系统能力**:SystemCapability.Communication.NetManager.Core 746 747**参数:** 748 749| 参数名 | 类型 | 必填 | 说明 | 750| --------- | ----------------------- | ---- | ---------------- | 751| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄。 | 752 753**返回值:** 754 755| 类型 | 说明 | 756| ------------------------------------------------------- | --------------------------------- | 757| Promise\<[ConnectionProperties](#connectionproperties)> | 以Promise形式返回网络的连接信息。 | 758 759**错误码:** 760 761| 错误码ID | 错误信息 | 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**示例:** 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 785获取netHandle对应的网络的连接信息,使用同步方法返回。 786 787**需要权限**:ohos.permission.GET_NETWORK_INFO 788 789**系统能力**:SystemCapability.Communication.NetManager.Core 790 791**参数:** 792 793| 参数名 | 类型 | 必填 | 说明 | 794| --------- | ----------------------- | ---- | ---------------- | 795| netHandle | [NetHandle](#nethandle8) | 是 | 数据网络的句柄。 | 796 797**返回值:** 798 799| 类型 | 说明 | 800| ------------------------------------------------------- | --------------------------------- | 801| [ConnectionProperties](#connectionproperties8) | 返回网络的连接信息。 | 802 803**错误码:** 804 805| 错误码ID | 错误信息 | 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**示例:** 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 826获取netHandle对应的网络的能力信息,使用callback方式作为异步方法。 827 828**需要权限**:ohos.permission.GET_NETWORK_INFO 829 830**系统能力**:SystemCapability.Communication.NetManager.Core 831 832**参数:** 833 834| 参数名 | 类型 | 必填 | 说明 | 835| --------- | --------------------------------------------------- | ---- | ------------------------------------------------------------ | 836| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄。 | 837| callback | AsyncCallback\<[NetCapabilities](#netcapabilities)> | 是 | 回调函数。当成功获取netHandle对应的网络的能力信息时,error为undefined,data为获取到的网络能力信息;否则为错误对象 | 838 839**错误码:** 840 841| 错误码ID | 错误信息 | 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**示例:** 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 867获取netHandle对应的网络的能力信息,使用Promise方式作为异步方法。 868 869**需要权限**:ohos.permission.GET_NETWORK_INFO 870 871**系统能力**:SystemCapability.Communication.NetManager.Core 872 873**参数:** 874 875| 参数名 | 类型 | 必填 | 说明 | 876| --------- | ----------------------- | ---- | ---------------- | 877| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄。 | 878 879**返回值:** 880 881| 类型 | 说明 | 882| --------------------------------------------- | --------------------------------- | 883| Promise\<[NetCapabilities](#netcapabilities)> | 以Promise形式返回网络的能力信息。 | 884 885**错误码:** 886 887| 错误码ID | 错误信息 | 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**示例:** 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 911获取netHandle对应的网络的能力信息,使用同步方式返回。 912 913**需要权限**:ohos.permission.GET_NETWORK_INFO 914 915**系统能力**:SystemCapability.Communication.NetManager.Core 916 917**参数:** 918 919| 参数名 | 类型 | 必填 | 说明 | 920| --------- | ----------------------- | ---- | ---------------- | 921| netHandle | [NetHandle](#nethandle8) | 是 | 数据网络的句柄。 | 922 923**返回值:** 924 925| 类型 | 说明 | 926| --------------------------------------------- | --------------------------------- | 927| [NetCapabilities](#netcapabilities8) | 返回网络的能力信息。 | 928 929**错误码:** 930 931| 错误码ID | 错误信息 | 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**示例:** 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 952检查当前网络上的数据流量使用是否被计量,使用callback方式作为异步方法。 953 954**需要权限**:ohos.permission.GET_NETWORK_INFO 955 956**系统能力**:SystemCapability.Communication.NetManager.Core 957 958**参数:** 959 960| 参数名 | 类型 | 必填 | 说明 | 961| -------- | ----------------------- | ---- | -------------------------------------- | 962| callback | AsyncCallback\<boolean> | 是 | 回调函数。当前网络上的数据流量使用被计量返回true。 | 963 964**错误码:** 965 966| 错误码ID | 错误信息 | 967| ------- | ----------------------------- | 968| 201 | Permission denied. | 969| 401 | Parameter error. | 970| 2100002 | Operation failed. Cannot connect to service.| 971| 2100003 | System internal error. | 972 973**示例:** 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 989检查当前网络上的数据流量使用是否被计量,使用Promise方式作为异步方法。 990 991**需要权限**:ohos.permission.GET_NETWORK_INFO 992 993**系统能力**:SystemCapability.Communication.NetManager.Core 994 995**返回值:** 996 997| 类型 | 说明 | 998| ----------------- | ----------------------------------------------- | 999| Promise\<boolean> | 以Promise形式返回,当前网络上的数据流量使用被计量true。 | 1000 1001**错误码:** 1002 1003| 错误码ID | 错误信息 | 1004| ------- | ----------------------------- | 1005| 201 | Permission denied. | 1006| 401 | Parameter error. | 1007| 2100002 | Operation failed. Cannot connect to service.| 1008| 2100003 | System internal error. | 1009 1010**示例:** 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 1024检查当前网络上的数据流量使用是否被计量,使用同步方式返回。 1025 1026**需要权限**:ohos.permission.GET_NETWORK_INFO 1027 1028**系统能力**:SystemCapability.Communication.NetManager.Core 1029 1030**返回值:** 1031 1032| 类型 | 说明 | 1033| ----------------- | ----------------------------------------------- | 1034| boolean | 当前网络上的数据流量使用被计量true。 | 1035 1036**错误码:** 1037 1038| 错误码ID | 错误信息 | 1039| ------- | ----------------------------- | 1040| 201 | Permission denied. | 1041| 401 | Parameter error. | 1042| 2100002 | Operation failed. Cannot connect to service.| 1043| 2100003 | System internal error. | 1044 1045**示例:** 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 1057检查默认数据网络是否被激活,使用callback方式作为异步方法。如果有默认数据网路,可以使用[getDefaultNet](#connectiongetdefaultnet)去获取。 1058 1059**需要权限**:ohos.permission.GET_NETWORK_INFO 1060 1061**系统能力**:SystemCapability.Communication.NetManager.Core 1062 1063**参数:** 1064 1065| 参数名 | 类型 | 必填 | 说明 | 1066| -------- | ----------------------- | ---- | -------------------------------------- | 1067| callback | AsyncCallback\<boolean> | 是 | 回调函数。默认数据网络被激活返回true。 | 1068 1069**错误码:** 1070 1071| 错误码ID | 错误信息 | 1072| ------- | ----------------------------- | 1073| 201 | Permission denied. | 1074| 401 | Parameter error. | 1075| 2100002 | Operation failed. Cannot connect to service.| 1076| 2100003 | System internal error. | 1077 1078**示例:** 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 1094检查默认数据网络是否被激活,使用Promise方式作为异步方法。如果有默认数据网路,可以使用[getDefaultNet](#connectiongetdefaultnet)去获取。 1095 1096**需要权限**:ohos.permission.GET_NETWORK_INFO 1097 1098**系统能力**:SystemCapability.Communication.NetManager.Core 1099 1100**返回值:** 1101 1102| 类型 | 说明 | 1103| ----------------- | ----------------------------------------------- | 1104| Promise\<boolean> | 以Promise形式返回,默认数据网络被激活返回true。 | 1105 1106**错误码:** 1107 1108| 错误码ID | 错误信息 | 1109| ------- | ----------------------------- | 1110| 201 | Permission denied. | 1111| 401 | Parameter error. | 1112| 2100002 | Operation failed. Cannot connect to service.| 1113| 2100003 | System internal error. | 1114 1115**示例:** 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 1128检查默认数据网络是否被激活,使用同步方式返回接口,如果被激活则返回true。 1129 1130**需要权限**:ohos.permission.GET_NETWORK_INFO 1131 1132**系统能力**:SystemCapability.Communication.NetManager.Core 1133 1134**返回值:** 1135 1136| 类型 | 说明 | 1137| ----------------- | ----------------------------------------------- | 1138| boolean | 默认数据网络被激活返回true。 | 1139 1140**错误码:** 1141 1142| 错误码ID | 错误信息 | 1143| ------- | ----------------------------- | 1144| 201 | Permission denied. | 1145| 401 | Parameter error. | 1146| 2100002 | Operation failed. Cannot connect to service.| 1147| 2100003 | System internal error. | 1148 1149**示例:** 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 1161开启飞行模式,使用callback方式作为异步方法。 1162 1163**系统接口**:此接口为系统接口。 1164 1165**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 1166 1167**系统能力**:SystemCapability.Communication.NetManager.Core 1168 1169**参数:** 1170 1171| 参数名 | 类型 | 必填 | 说明 | 1172| -------- | ------------------------------------------------- | ---- | ------------------ | 1173| callback | AsyncCallback\<void> | 是 | 回调函数。 | 1174 1175**错误码:** 1176 1177| 错误码ID | 错误信息 | 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**示例:** 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 1200开启飞行模式,使用Promise方式作为异步方法。 1201 1202**系统接口**:此接口为系统接口。 1203 1204**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 1205 1206**系统能力**:SystemCapability.Communication.NetManager.Core 1207 1208**返回值:** 1209 1210| 类型 | 说明 | 1211| ------------------------------------------- | ----------------------------- | 1212| Promise\<void> | 无返回值的Promise对象。 | 1213 1214**错误码:** 1215 1216| 错误码ID | 错误信息 | 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**示例:** 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 1238关闭飞行模式,使用callback方式作为异步方法。 1239 1240**系统接口**:此接口为系统接口。 1241 1242**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 1243 1244**系统能力**:SystemCapability.Communication.NetManager.Core 1245 1246**参数:** 1247 1248| 参数名 | 类型 | 必填 | 说明 | 1249| -------- | ------------------------------------------------- | ---- | ------------------ | 1250| callback | AsyncCallback\<void> | 是 | 回调函数。当关闭飞行模式成功,error为undefined,否则为错误对象。 | 1251 1252**错误码:** 1253 1254| 错误码ID | 错误信息 | 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**示例:** 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 1277关闭飞行模式,使用Promise方式作为异步方法。 1278 1279**系统接口**:此接口为系统接口。 1280 1281**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 1282 1283**系统能力**:SystemCapability.Communication.NetManager.Core 1284 1285**返回值:** 1286 1287| 类型 | 说明 | 1288| ------------------------------------------- | ----------------------------- | 1289| Promise\<void> | 无返回值的Promise对象。 | 1290 1291**错误码:** 1292 1293| 错误码ID | 错误信息 | 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**示例:** 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 1315向网络管理报告网络处于可用状态,使用callback方式作为异步方法。 1316 1317**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET 1318 1319**系统能力**:SystemCapability.Communication.NetManager.Core 1320 1321**参数:** 1322 1323| 参数名 | 类型 | 必填 | 说明 | 1324| -------- | -------- | -------- | -------- | 1325| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 | 1326| callback | AsyncCallback<void> | 是 | 回调函数。当向网络管理报告网络处于可用状态成功,error为undefined,否则为错误对象。 | 1327 1328**错误码:** 1329 1330| 错误码ID | 错误信息 | 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**示例:** 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 1355向网络管理报告网络处于可用状态,使用Promise方式作为异步方法。 1356 1357**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET 1358 1359**系统能力**:SystemCapability.Communication.NetManager.Core 1360 1361**参数:** 1362 1363| 参数名 | 类型 | 必填 | 说明 | 1364| -------- | -------- | -------- | -------- | 1365| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 | 1366 1367**返回值:** 1368| 类型 | 说明 | 1369| -------- | -------- | 1370| Promise<void> | 无返回值的Promise对象。 | 1371 1372**错误码:** 1373 1374| 错误码ID | 错误信息 | 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**示例:** 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 1397向网络管理报告网络处于不可用状态,使用callback方式作为异步方法。 1398 1399**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET 1400 1401**系统能力**:SystemCapability.Communication.NetManager.Core 1402 1403**参数:** 1404 1405| 参数名 | 类型 | 必填 | 说明 | 1406| -------- | -------- | -------- | -------- | 1407| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 | 1408| callback | AsyncCallback<void> | 是 | 回调函数。当向网络管理报告网络处于不可用状态成功,error为undefined,否则为错误对象。 | 1409 1410**错误码:** 1411 1412| 错误码ID | 错误信息 | 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**示例:** 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 1435向网络管理报告网络处于不可用状态,使用Promise方式作为异步方法。 1436 1437**需要权限**:ohos.permission.GET_NETWORK_INFO 和 ohos.permission.INTERNET 1438 1439**系统能力**:SystemCapability.Communication.NetManager.Core 1440 1441**参数:** 1442 1443| 参数名 | 类型 | 必填 | 说明 | 1444| -------- | -------- | -------- | -------- | 1445| netHandle | [NetHandle](#nethandle) | 是 | 数据网络的句柄,参考[NetHandle](#nethandle)。 | 1446 1447**返回值:** 1448| 类型 | 说明 | 1449| -------- | -------- | 1450| Promise<void> | 无返回值的Promise对象。 | 1451 1452**错误码:** 1453 1454| 错误码ID | 错误信息 | 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**示例:** 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 1477使用默认网络解析主机名以获取所有IP地址,使用callback方式作为异步方法。 1478 1479**需要权限**:ohos.permission.INTERNET 1480 1481**系统能力**:SystemCapability.Communication.NetManager.Core 1482 1483**参数:** 1484 1485| 参数名 | 类型 | 必填 | 说明 | 1486| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ | 1487| host | string | 是 | 需要解析的主机名。 | 1488| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | 是 | 回调函数。当使用默认网络解析主机名成功获取所有IP地址,error为undefined,data为获取到的所有IP地址;否则为错误对象。 | 1489 1490**错误码:** 1491 1492| 错误码ID | 错误信息 | 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**示例:** 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 1515使用默认网络解析主机名以获取所有IP地址,使用Promise方式作为异步方法。 1516 1517**需要权限**:ohos.permission.INTERNET 1518 1519**系统能力**:SystemCapability.Communication.NetManager.Core 1520 1521**参数:** 1522 1523| 参数名 | 类型 | 必填 | 说明 | 1524| ------ | ------ | ---- | ------------------ | 1525| host | string | 是 | 需要解析的主机名。 | 1526 1527**返回值:** 1528 1529| 类型 | 说明 | 1530| ------------------------------------------- | ----------------------------- | 1531| Promise\<Array\<[NetAddress](#netaddress)>> | 以Promise形式返回所有IP地址。 | 1532 1533**错误码:** 1534 1535| 错误码ID | 错误信息 | 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**示例:** 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 1554网络连接的句柄。 1555 1556> **说明:** 1557> 设备从无网络到有网络会触发netAvailable事件、netCapabilitiesChange事件和netConnectionPropertiesChange事件; 1558> 设备从有网络到无网络状态会触发netLost事件; 1559> 设备从WiFi到蜂窝会触发netLost事件(WiFi丢失)之后触发 netAvaliable事件(蜂窝可用); 1560 1561### register<sup>8+</sup> 1562 1563register(callback: AsyncCallback\<void>): void 1564 1565订阅指定网络状态变化的通知。 1566 1567**需要权限**:ohos.permission.GET_NETWORK_INFO 1568 1569**系统能力**:SystemCapability.Communication.NetManager.Core 1570 1571**参数:** 1572 1573| 参数名 | 类型 | 必填 | 说明 | 1574| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1575| callback | AsyncCallback\<void> | 是 | 回调函数。当订阅指定网络状态变化的通知成功,error为undefined,否则为错误对象。 | 1576 1577**错误码:** 1578 1579| 错误码ID | 错误信息 | 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**示例:** 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 1603取消订阅默认网络状态变化的通知。 1604 1605**系统能力**:SystemCapability.Communication.NetManager.Core 1606 1607**参数:** 1608 1609| 参数名 | 类型 | 必填 | 说明 | 1610| -------- | -------------------- | ---- | ------------------------------------------------------------ | 1611| callback | AsyncCallback\<void> | 是 | 回调函数。当取消订阅指定网络状态变化的通知成功,error为undefined,否则为错误对象。 | 1612 1613**错误码:** 1614 1615| 错误码ID | 错误信息 | 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**示例:** 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 1638订阅网络可用事件。 1639 1640**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。 1641 1642**系统能力**:SystemCapability.Communication.NetManager.Core 1643 1644**参数:** 1645 1646| 参数名 | 类型 | 必填 | 说明 | 1647| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 1648| type | string | 是 | 订阅事件,固定为'netAvailable'。<br>netAvailable:数据网络可用事件。 | 1649| callback | Callback\<[NetHandle](#nethandle)> | 是 | 回调函数,返回数据网络句柄。| 1650 1651**示例:** 1652 1653```ts 1654import connection from '@ohos.net.connection' 1655import { BusinessError } from "@ohos.base" 1656 1657// 创建NetConnection对象 1658let netCon: connection.NetConnection = connection.createNetConnection(); 1659 1660// 先使用register接口注册订阅事件 1661netCon.register((error: BusinessError) => { 1662 console.log(JSON.stringify(error)) 1663}) 1664 1665// 订阅网络可用事件。调用register后,才能接收到此事件通知 1666netCon.on('netAvailable', (data: connection.NetHandle) => { 1667 console.log(JSON.stringify(data)) 1668}) 1669 1670// 使用unregister接口取消订阅 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 1680订阅网络阻塞状态事件,使用callback方式作为异步方法。 1681 1682**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。 1683 1684**系统能力**:SystemCapability.Communication.NetManager.Core 1685 1686**参数:** 1687 1688| 参数名 | 类型 | 必填 | 说明 | 1689| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1690| type | string | 是 | 订阅事件,固定为'netBlockStatusChange'。<br/>netBlockStatusChange:网络阻塞状态事件。 | 1691| callback | Callback<{ netHandle: [NetHandle](#nethandle), blocked: boolean }> | 是 | 回调函数,返回数据网络句柄(netHandle),及网络堵塞状态(blocked)。| 1692 1693**示例:** 1694 1695```ts 1696import connection from '@ohos.net.connection' 1697import { BusinessError } from "@ohos.base" 1698 1699// 创建NetConnection对象 1700let netCon: connection.NetConnection = connection.createNetConnection(); 1701 1702// 先使用register接口注册订阅事件 1703netCon.register((error: BusinessError) => { 1704 console.log(JSON.stringify(error)) 1705}) 1706 1707// 订阅网络可用事件。调用register后,才能接收到此事件通知 1708netCon.on('netAvailable', (data: connection.NetHandle) => { 1709 console.log(JSON.stringify(data)) 1710}) 1711 1712// 使用unregister接口取消订阅 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 1722订阅网络能力变化事件。 1723 1724**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。 1725 1726**系统能力**:SystemCapability.Communication.NetManager.Core 1727 1728**参数:** 1729 1730| 参数名 | 类型 | 必填 | 说明 | 1731| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1732| type | string | 是 | 订阅事件,固定为'netCapabilitiesChange'。<br/>netCapabilitiesChange:网络能力变化事件。 | 1733| callback | Callback<[NetCapabilityInfo](#netcapabilityinfo)> | 是 | 回调函数,返回数据网络句柄(netHandle)和网络的能力信息(netCap)。| 1734 1735**示例:** 1736 1737```ts 1738import connection from '@ohos.net.connection' 1739import { BusinessError } from "@ohos.base" 1740 1741// 创建NetConnection对象 1742let netCon: connection.NetConnection = connection.createNetConnection(); 1743 1744// 先使用register接口注册订阅事件 1745netCon.register((error: BusinessError) => { 1746 console.log(JSON.stringify(error)) 1747}) 1748 1749// 订阅网络可用事件。调用register后,才能接收到此事件通知 1750netCon.on('netAvailable', (data: connection.NetHandle) => { 1751 console.log(JSON.stringify(data)) 1752}) 1753 1754// 使用unregister接口取消订阅 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 1765订阅网络连接信息变化事件。 1766 1767**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。 1768 1769**系统能力**:SystemCapability.Communication.NetManager.Core 1770 1771**参数:** 1772 1773| 参数名 | 类型 | 必填 | 说明 | 1774| -------- | ------------------------------------------------------------ | ---- | ------------------------------------------------------------ | 1775| type | string | 是 | 订阅事件,固定为'netConnectionPropertiesChange'。<br/>netConnectionPropertiesChange:网络连接信息变化事件。 | 1776| callback | Callback<{ netHandle: [NetHandle](#nethandle), connectionProperties: [ConnectionProperties](#connectionproperties) }> | 是 | 回调函数,返回数据网络句柄(netHandle)和网络的连接信息(connectionProperties)| 1777 1778**示例:** 1779 1780```ts 1781import connection from '@ohos.net.connection' 1782import { BusinessError } from "@ohos.base" 1783 1784// 创建NetConnection对象 1785let netCon: connection.NetConnection = connection.createNetConnection(); 1786 1787// 先使用register接口注册订阅事件 1788netCon.register((error: BusinessError) => { 1789 console.log(JSON.stringify(error)) 1790}) 1791 1792// 订阅网络可用事件。调用register后,才能接收到此事件通知 1793netCon.on('netAvailable', (data: connection.NetHandle) => { 1794 console.log(JSON.stringify(data)) 1795}) 1796 1797// 使用unregister接口取消订阅 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 1807订阅网络丢失事件。 1808 1809**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。 1810 1811**系统能力**:SystemCapability.Communication.NetManager.Core 1812 1813**参数:** 1814 1815| 参数名 | 类型 | 必填 | 说明 | 1816| -------- | ---------------------------------- | ---- | ------------------------------------------------------------ | 1817| type | string | 是 | 订阅事件,固定为'netLost'。<br/>netLost:网络严重中断或正常断开事件。 | 1818| callback | Callback\<[NetHandle](#nethandle)> | 是 | 回调函数,数据网络句柄(netHandle)| 1819 1820**示例:** 1821 1822```ts 1823import connection from '@ohos.net.connection' 1824import { BusinessError } from "@ohos.base" 1825 1826// 创建NetConnection对象 1827let netCon: connection.NetConnection = connection.createNetConnection(); 1828 1829// 先使用register接口注册订阅事件 1830netCon.register((error: BusinessError) => { 1831 console.log(JSON.stringify(error)) 1832}) 1833 1834// 订阅网络可用事件。调用register后,才能接收到此事件通知 1835netCon.on('netAvailable', (data: connection.NetHandle) => { 1836 console.log(JSON.stringify(data)) 1837}) 1838 1839// 使用unregister接口取消订阅 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 1849订阅网络不可用事件。 1850 1851**模型约束**:此接口调用之前需要先调用register接口,使用unregister取消订阅默认网络状态变化的通知。 1852 1853**系统能力**:SystemCapability.Communication.NetManager.Core 1854 1855**参数:** 1856 1857| 参数名 | 类型 | 必填 | 说明 | 1858| -------- | --------------- | ---- | ------------------------------------------------------------ | 1859| type | string | 是 | 订阅事件,固定为'netUnavailable'。<br/>netUnavailable:网络不可用事件。 | 1860| callback | Callback\<void> | 是 | 回调函数,无返回结果。| 1861 1862**示例:** 1863 1864```ts 1865import connection from '@ohos.net.connection' 1866import { BusinessError } from "@ohos.base" 1867 1868// 创建NetConnection对象 1869let netCon: connection.NetConnection = connection.createNetConnection(); 1870 1871// 先使用register接口注册订阅事件 1872netCon.register((error: BusinessError) => { 1873 console.log(JSON.stringify(error)) 1874}) 1875 1876// 订阅网络可用事件。调用register后,才能接收到此事件通知 1877netCon.on('netAvailable', (data: connection.NetHandle) => { 1878 console.log(JSON.stringify(data)) 1879}) 1880 1881// 使用unregister接口取消订阅 1882netCon.unregister((error: BusinessError) => { 1883 console.log(JSON.stringify(error)) 1884}) 1885``` 1886 1887## NetHandle<sup>8+</sup> 1888 1889数据网络的句柄。 1890 1891在调用NetHandle的方法之前,需要先获取NetHandle对象。 1892 1893**系统能力**:SystemCapability.Communication.NetManager.Core 1894 1895### 属性 1896 1897| 名称 | 类型 | 必填 | 说明 | 1898| ------ | ------ | --- |------------------------- | 1899| netId | number | 是 | 网络ID,取值为0代表没有默认网络,其余取值必须大于等于100。 | 1900 1901### bindSocket<sup>9+</sup> 1902 1903bindSocket(socketParam: TCPSocket \| UDPSocket, callback: AsyncCallback\<void>): void 1904 1905将TCPSocket或UDPSocket绑定到当前网络,使用callback方式作为异步方法。 1906 1907**系统能力**:SystemCapability.Communication.NetManager.Core 1908 1909**参数:** 1910 1911| 参数名 | 类型 | 必填 | 说明 | 1912| ----------- | ------------------------ | ---- | -------------------------------| 1913| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | 是 | 待绑定的TCPSocket或UDPSocket对象。| 1914| callback | AsyncCallback\<void> | 是 | 回调函数。当TCPSocket或UDPSocket成功绑定到当前网络,error为undefined,否则为错误对象。 | 1915 1916**错误码:** 1917 1918| 错误码ID | 错误信息 | 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**示例:** 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 1987将TCPSocket或UDPSockett绑定到当前网络,使用Promise方式作为异步方法。 1988 1989**系统能力**:SystemCapability.Communication.NetManager.Core 1990 1991**参数:** 1992 1993| 参数名 | 类型 | 必填 | 说明 | 1994| --------------- | --------------------- | ---- | ------------------------------ | 1995| socketParam | [TCPSocket](js-apis-socket.md#tcpsocket) \| [UDPSocket](js-apis-socket.md#udpsocket) | 是 | 待绑定的TCPSocket或UDPSocket对象。| 1996 1997**返回值:** 1998 1999| 类型 | 说明 | 2000| -------------- | ---------------------- | 2001| Promise\<void> | 无返回值的Promise对象。 | 2002 2003**错误码:** 2004 2005| 错误码ID | 错误信息 | 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**示例:** 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 2073使用对应网络解析主机名以获取所有IP地址,使用callback方式作为异步方法。 2074 2075**需要权限**:ohos.permission.INTERNET 2076 2077**系统能力**:SystemCapability.Communication.NetManager.Core 2078 2079**参数:** 2080 2081| 参数名 | 类型 | 必填 | 说明 | 2082| -------- | ------------------------------------------------- | ---- | ------------------------------------------------------------ | 2083| host | string | 是 | 需要解析的主机名。 | 2084| callback | AsyncCallback\<Array\<[NetAddress](#netaddress)>> | 是 | 回调函数。当使用对应网络解析主机名成功获取所有IP地址,error为undefined,data为获取到的所有IP地址;否则为错误对象。 | 2085 2086**错误码:** 2087 2088| 错误码ID | 错误信息 | 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**示例:** 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 2115使用对应网络解析主机名以获取所有IP地址,使用Promise方式作为异步方法。 2116 2117**需要权限**:ohos.permission.INTERNET 2118 2119**系统能力**:SystemCapability.Communication.NetManager.Core 2120 2121**参数:** 2122 2123| 参数名 | 类型 | 必填 | 说明 | 2124| ------ | ------ | ---- | ------------------ | 2125| host | string | 是 | 需要解析的主机名。 | 2126 2127**返回值:** 2128 2129| 类型 | 说明 | 2130| ------------------------------------------- | ----------------------------- | 2131| Promise\<Array\<[NetAddress](#netaddress)>> | 以Promise形式返回所有IP地址。 | 2132 2133**错误码:** 2134 2135| 错误码ID | 错误信息 | 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**示例:** 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 2160使用对应网络解析主机名以获取第一个IP地址,使用callback方式作为异步方法。 2161 2162**需要权限**:ohos.permission.INTERNET 2163 2164**系统能力**:SystemCapability.Communication.NetManager.Core 2165 2166**参数:** 2167 2168| 参数名 | 类型 | 必填 | 说明 | 2169| -------- | ----------------------------------------- | ---- | ------------------------------------------------------------ | 2170| host | string | 是 | 需要解析的主机名。 | 2171| callback | AsyncCallback\<[NetAddress](#netaddress)> | 是 | 回调函数。当使用对应网络解析主机名获取第一个IP地址成功,error为undefined,data为获取的第一个IP地址;否则为错误对象。 | 2172 2173**错误码:** 2174 2175| 错误码ID | 错误信息 | 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**示例:** 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 2202使用对应网络解析主机名以获取第一个IP地址,使用Promise方式作为异步方法。 2203 2204**需要权限**:ohos.permission.INTERNET 2205 2206**系统能力**:SystemCapability.Communication.NetManager.Core 2207 2208**参数:** 2209 2210| 参数名 | 类型 | 必填 | 说明 | 2211| ------ | ------ | ---- | ------------------ | 2212| host | string | 是 | 需要解析的主机名。 | 2213 2214**返回值:** 2215 2216| 类型 | 说明 | 2217| ----------------------------------- | ------------------------------- | 2218| Promise\<[NetAddress](#netaddress)> | 以Promise形式返回第一个IP地址。 | 2219 2220**错误码:** 2221 2222| 错误码ID | 错误信息 | 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**示例:** 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 2245网络具体能力。 2246 2247**系统能力**:SystemCapability.Communication.NetManager.Core 2248 2249| 名称 | 值 | 说明 | 2250| ------------------------ | ---- | ---------------------- | 2251| NET_CAPABILITY_MMS | 0 | 表示网络可以访问运营商的MMSC(Multimedia Message Service,多媒体短信服务)发送和接收彩信。 | 2252| NET_CAPABILITY_NOT_METERED | 11 | 表示网络流量未被计费。 | 2253| NET_CAPABILITY_INTERNET | 12 | 表示该网络应具有访问Internet的能力,该能力由网络提供者设置。 | 2254| NET_CAPABILITY_NOT_VPN | 15 | 表示网络不使用VPN(Virtual Private Network,虚拟专用网络)。 | 2255| NET_CAPABILITY_VALIDATED | 16 | 表示该网络访问Internet的能力被网络管理成功验证,该能力由网络管理模块设置。 | 2256 2257## NetBearType<sup>8+</sup> 2258 2259网络类型。 2260 2261**系统能力**:SystemCapability.Communication.NetManager.Core 2262 2263| 名称 | 值 | 说明 | 2264| --------------- | ---- | ----------- | 2265| BEARER_CELLULAR | 0 | 蜂窝网络。 | 2266| BEARER_WIFI | 1 | Wi-Fi网络。 | 2267| BEARER_ETHERNET | 3 | 以太网网络。 | 2268 2269## HttpProxy<sup>10+</sup> 2270 2271网络代理配置信息 2272 2273**系统能力**:SystemCapability.Communication.NetManager.Core 2274 2275| 名称 | 类型 | 必填 | 说明 | 2276| ------ | ------ | --- |------------------------- | 2277| host | string | 否 | 代理服务器主机名。 | 2278| port | number | 否 | 主机端口。 | 2279| exclusionList | Array<string> | 否 | 不使用代理的主机名列表,主机名支持域名、IP地址以及通配符形式,详细匹配规则如下:<br/>1、域名匹配规则:<br/>(1)完全匹配:代理服务器主机名只要与列表中的任意一个主机名完全相同,就可以匹配。<br/>(2)包含匹配:代理服务器主机名只要包含列表中的任意一个主机名,就可以匹配。<br/>例如,如果在主机名列表中设置了 “ample.com”,则 “ample.com”、“www.ample.com”、“ample.com:80”都会被匹配,而 “www.example.com”、“ample.com.org”则不会被匹配。<br/>2、IP地址匹配规则:代理服务器主机名只要与列表中的任意一个IP地址完全相同,就可以匹配。<br/>3、域名跟IP地址可以同时添加到列表中进行匹配。<br/>4、单个“*”是唯一有效的通配符,当列表中只有通配符时,将与所有代理服务器主机名匹配,表示禁用代理。通配符只能单独添加,不可以与其他域名、IP地址一起添加到列表中,否则通配符将不生效。<br/>5、匹配规则不区分主机名大小写。<br/>6、匹配主机名时,不考虑http和https等协议前缀。 | 2280 2281## NetSpecifier<sup>8+</sup> 2282 2283提供承载数据网络能力的实例。 2284 2285**系统能力**:SystemCapability.Communication.NetManager.Core 2286 2287| 名称 | 类型 | 必填 | 说明 | 2288| ----------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 2289| netCapabilities | [NetCapabilities](#netcapabilities) | 是 | 存储数据网络的传输能力和承载类型。 | 2290| bearerPrivateIdentifier | string | 否 | 网络标识符,Wi-Fi网络的标识符是"wifi",蜂窝网络的标识符是"slot0"(对应SIM卡1)。 | 2291 2292## NetCapabilityInfo<sup>10+</sup> 2293 2294提供承载数据网络能力的实例。 2295 2296**系统能力**:SystemCapability.Communication.NetManager.Core 2297 2298| 名称 | 类型 | 必填 | 说明 | 2299| ----------------------- | ----------------------------------- | ---- | ------------------------------------------------------------ | 2300| netHandle | [NetHandle](#nethandle) | 是 | 数据网络句柄。 | 2301| netCap | [NetCapabilities](#netcapabilities) | 否 | 存储数据网络的传输能力和承载类型。 | 2302 2303## NetCapabilities<sup>8+</sup> 2304 2305网络的能力集。 2306 2307**系统能力**:SystemCapability.Communication.NetManager.Core 2308 2309| 名称 | 类型 | 必填 | 说明 | 2310| --------------------- | ---------------------------------- | --- | ------------------------ | 2311| linkUpBandwidthKbps | number | 否 | 上行(设备到网络)带宽,0表示无法评估当前网络带宽。 | 2312| linkDownBandwidthKbps | number | 否 | 下行(网络到设备)带宽,0表示无法评估当前网络带宽。 | 2313| networkCap | Array\<[NetCap](#netcap)> | 否 | 网络具体能力。 | 2314| bearerTypes | Array\<[NetBearType](#netbeartype)> | 是 | 网络类型。 | 2315 2316## ConnectionProperties<sup>8+</sup> 2317 2318网络连接信息。 2319 2320**系统能力**:SystemCapability.Communication.NetManager.Core 2321 2322| 名称 | 类型 | 必填 | 说明 | 2323| ------------- | ---------------------------------- | ----|---------------- | 2324| interfaceName | string | 是 |网卡名称。 | 2325| domains | string | 是 |所属域,默认""。 | 2326| linkAddresses | Array\<[LinkAddress](#linkaddress)> | 是 |链路信息。 | 2327| routes | Array\<[RouteInfo](#routeinfo)> | 是 |路由信息。 | 2328| dnses | Array\<[NetAddress](#netaddress)> | 是 |网络地址,参考[NetAddress](#netaddress)。 | 2329| mtu | number | 是 |最大传输单元。 | 2330 2331## RouteInfo<sup>8+</sup> 2332 2333网络路由信息。 2334 2335**系统能力**:SystemCapability.Communication.NetManager.Core 2336 2337| 名称 | 类型 | 必填 |说明 | 2338| -------------- | --------------------------- | --- |---------------- | 2339| interface | string | 是 |网卡名称。 | 2340| destination | [LinkAddress](#linkaddress) | 是 |目的地址。 | 2341| gateway | [NetAddress](#netaddress) | 是 |网关地址。 | 2342| hasGateway | boolean | 是 |是否有网关。 | 2343| isDefaultRoute | boolean | 是 |是否为默认路由。 | 2344 2345## LinkAddress<sup>8+</sup> 2346 2347网络链路信息。 2348 2349**系统能力**:SystemCapability.Communication.NetManager.Core 2350 2351| 名称 | 类型 | 必填 |说明 | 2352| ------------ | ----------------------- |---- |-------------------- | 2353| address | [NetAddress](#netaddress) | 是 | 链路地址。 | 2354| prefixLength | number | 是 |链路地址前缀的长度。 | 2355 2356## NetAddress<sup>8+</sup> 2357 2358网络地址。 2359 2360**系统能力**:SystemCapability.Communication.NetManager.Core 2361 2362| 名称 | 类型 | 必填 | 说明 | 2363| ------- | ------ | -- |------------------------------ | 2364| address | string | 是 |地址。 | 2365| family | number | 否 |IPv4 = 1,IPv6 = 2,默认IPv4。 | 2366| port | number | 否 |端口,取值范围\[0, 65535]。 | 2367