1# @ohos.net.connection (网络连接管理)(系统接口) 2 3网络连接管理提供管理网络一些基础能力,包括获取默认激活的数据网络、获取所有激活数据网络列表、开启关闭飞行模式、获取网络能力信息等功能。 4 5> **说明:** 6> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 7> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.net.connection (网络连接管理)](js-apis-net-connection.md) 8 9## 导入模块 10 11```ts 12import connection from '@ohos.net.connection'; 13``` 14 15 16## connection.getGlobalHttpProxy<sup>10+</sup> 17 18getGlobalHttpProxy(callback: AsyncCallback\<HttpProxy>): void 19 20获取网络的全局代理配置信息,使用callback方式作为异步方法。 21 22**系统接口**:此接口为系统接口。 23 24**系统能力**:SystemCapability.Communication.NetManager.Core 25 26**参数:** 27 28| 参数名 | 类型 | 必填 | 说明 | 29| -------- | --------------------------------------- | ---- | ------------------------------------------------------------ | 30| callback | AsyncCallback\<[HttpProxy](js-apis-net-connection.md#httpproxy10)> | 是 | 回调函数。当成功获取网络的全局代理配置信息时,error为undefined,data为网络的全局代理配置信息;否则为错误对象 | 31 32**错误码:** 33 34| 错误码ID | 错误信息 | 35| ------- | ----------------------------- | 36| 401 | Parameter error. | 37| 202 | Non-system applications use system APIs. | 38| 2100002 | Operation failed. Cannot connect to service.| 39| 2100003 | System internal error. | 40 41**示例:** 42 43```ts 44import connection from '@ohos.net.connection'; 45import { BusinessError } from '@ohos.base'; 46 47connection.getGlobalHttpProxy((error: BusinessError, data: connection.HttpProxy) => { 48 console.info(JSON.stringify(error)); 49 console.info(JSON.stringify(data)); 50}); 51``` 52 53## connection.getGlobalHttpProxy<sup>10+</sup> 54 55getGlobalHttpProxy(): Promise\<HttpProxy>; 56 57获取网络的全局代理配置信息,使用Promise方式作为异步方法。 58 59**系统接口**:此接口为系统接口。 60 61**系统能力**:SystemCapability.Communication.NetManager.Core 62 63**返回值:** 64 65| 类型 | 说明 | 66| --------------------------------- | ------------------------------------- | 67| Promise\<[HttpProxy](js-apis-net-connection.md#httpproxy10)> | 以Promise形式返回网络的全局代理配置信息。 | 68 69**错误码:** 70 71| 错误码ID | 错误信息 | 72| ------- | ----------------------------- | 73| 401 | Parameter error. | 74| 202 | Non-system applications use system APIs. | 75| 2100002 | Operation failed. Cannot connect to service.| 76| 2100003 | System internal error. | 77 78**示例:** 79 80```ts 81import connection from '@ohos.net.connection'; 82import { BusinessError } from '@ohos.base'; 83 84connection.getGlobalHttpProxy().then((data: connection.HttpProxy) => { 85 console.info(JSON.stringify(data)); 86}).catch((error: BusinessError) => { 87 console.info(JSON.stringify(error)); 88}); 89``` 90 91## connection.setGlobalHttpProxy<sup>10+</sup> 92 93setGlobalHttpProxy(httpProxy: HttpProxy, callback: AsyncCallback\<void>): void 94 95设置网络全局Http代理配置信息,使用callback方式作为异步方法。 96 97**系统接口**:此接口为系统接口。 98 99**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 100 101**系统能力**:SystemCapability.Communication.NetManager.Core 102 103**参数:** 104 105| 参数名 | 类型 | 必填 | 说明 | 106| --------- | ----------------------- | ---- | ------------------------------------------------------------ | 107| httpProxy | [HttpProxy](js-apis-net-connection.md#httpproxy10) | 是 | 网络全局Http代理配置信息。 | 108| callback | AsyncCallback\<void> | 是 | 回调函数。当成功设置网络全局Http代理配置信息时,error为undefined,否则为错误对象。| 109 110**错误码:** 111 112| 错误码ID | 错误信息 | 113| ------- | ----------------------------- | 114| 201 | Permission denied. | 115| 401 | Parameter error. | 116| 202 | Non-system applications use system APIs. | 117| 2100001 | Invalid parameter value. | 118| 2100002 | Operation failed. Cannot connect to service.| 119| 2100003 | System internal error. | 120 121**示例:** 122 123```ts 124import connection from '@ohos.net.connection'; 125import { BusinessError } from '@ohos.base'; 126 127let exclusionStr = "192.168,baidu.com"; 128let exclusionArray = exclusionStr.split(','); 129let httpProxy: connection.HttpProxy = { 130 host: "192.168.xx.xxx", 131 port: 8080, 132 exclusionList: exclusionArray 133} 134connection.setGlobalHttpProxy(httpProxy, (err: BusinessError) => { 135 if (err) { 136 console.error(`setGlobalHttpProxy failed, callback: err->${JSON.stringify(err)}`); 137 return; 138 } 139 console.log(`setGlobalHttpProxy success.`); 140}); 141``` 142 143## connection.setGlobalHttpProxy<sup>10+</sup> 144 145setGlobalHttpProxy(httpProxy: HttpProxy): Promise\<void>; 146 147设置网络全局Http代理配置信息,使用Promise方式作为异步方法。 148 149**系统接口**:此接口为系统接口。 150 151**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 152 153**系统能力**:SystemCapability.Communication.NetManager.Core 154 155**参数:** 156 157| 参数名 | 类型 | 必填 | 说明 | 158| --------- | ------------------------------------------------------------ | ---- | ---------------- | 159| httpProxy | [HttpProxy](js-apis-net-connection.md#httpproxy10) | 是 | 网络全局Http代理配置信息。 | 160 161**返回值:** 162 163| 类型 | 说明 | 164| ------------------------------------------- | ----------------------------- | 165| Promise\<void> | 无返回值的Promise对象。 | 166 167**错误码:** 168 169| 错误码ID | 错误信息 | 170| ------- | ----------------------------- | 171| 201 | Permission denied. | 172| 401 | Parameter error. | 173| 202 | Non-system applications use system APIs. | 174| 2100001 | Invalid parameter value. | 175| 2100002 | Operation failed. Cannot connect to service.| 176| 2100003 | System internal error. | 177 178**示例:** 179 180```ts 181import connection from '@ohos.net.connection'; 182import { BusinessError } from '@ohos.base'; 183 184let exclusionStr = "192.168,baidu.com"; 185let exclusionArray = exclusionStr.split(','); 186connection.setGlobalHttpProxy({ 187 host: "192.168.xx.xxx", 188 port: 8080, 189 exclusionList: exclusionArray 190} as connection.HttpProxy).then(() => { 191 console.info("success"); 192}).catch((error: BusinessError) => { 193 console.info(JSON.stringify(error)); 194}); 195``` 196 197 198## connection.enableAirplaneMode 199 200enableAirplaneMode(callback: AsyncCallback\<void>): void 201 202开启飞行模式,使用callback方式作为异步方法。 203 204**系统接口**:此接口为系统接口。 205 206**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 207 208**系统能力**:SystemCapability.Communication.NetManager.Core 209 210**参数:** 211 212| 参数名 | 类型 | 必填 | 说明 | 213| -------- | ------------------------------------------------- | ---- | ------------------ | 214| callback | AsyncCallback\<void> | 是 | 回调函数。 | 215 216**错误码:** 217 218| 错误码ID | 错误信息 | 219| ------- | ----------------------------- | 220| 201 | Permission denied. | 221| 202 | Non-system applications use system APIs. | 222| 401 | Parameter error. | 223| 2100002 | Operation failed. Cannot connect to service.| 224| 2100003 | System internal error. | 225 226**示例:** 227 228```ts 229import connection from '@ohos.net.connection'; 230import { BusinessError } from '@ohos.base'; 231 232connection.enableAirplaneMode((error: BusinessError) => { 233 console.log(JSON.stringify(error)); 234}); 235``` 236 237## connection.enableAirplaneMode 238 239enableAirplaneMode(): Promise\<void> 240 241开启飞行模式,使用Promise方式作为异步方法。 242 243**系统接口**:此接口为系统接口。 244 245**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 246 247**系统能力**:SystemCapability.Communication.NetManager.Core 248 249**返回值:** 250 251| 类型 | 说明 | 252| ------------------------------------------- | ----------------------------- | 253| Promise\<void> | 无返回值的Promise对象。 | 254 255**错误码:** 256 257| 错误码ID | 错误信息 | 258| ------- | ----------------------------- | 259| 201 | Permission denied. | 260| 202 | Non-system applications use system APIs. | 261| 401 | Parameter error. | 262| 2100002 | Operation failed. Cannot connect to service.| 263| 2100003 | System internal error. | 264 265**示例:** 266 267```ts 268import connection from '@ohos.net.connection'; 269 270connection.enableAirplaneMode().then((error: void) => { 271 console.log(JSON.stringify(error)); 272}); 273``` 274 275## connection.disableAirplaneMode 276 277disableAirplaneMode(callback: AsyncCallback\<void>): void 278 279关闭飞行模式,使用callback方式作为异步方法。 280 281**系统接口**:此接口为系统接口。 282 283**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 284 285**系统能力**:SystemCapability.Communication.NetManager.Core 286 287**参数:** 288 289| 参数名 | 类型 | 必填 | 说明 | 290| -------- | ------------------------------------------------- | ---- | ------------------ | 291| callback | AsyncCallback\<void> | 是 | 回调函数。当关闭飞行模式成功,error为undefined,否则为错误对象。 | 292 293**错误码:** 294 295| 错误码ID | 错误信息 | 296| ------- | ----------------------------- | 297| 201 | Permission denied. | 298| 202 | Non-system applications use system APIs. | 299| 401 | Parameter error. | 300| 2100002 | Operation failed. Cannot connect to service.| 301| 2100003 | System internal error. | 302 303**示例:** 304 305```ts 306import connection from '@ohos.net.connection'; 307import { BusinessError } from '@ohos.base'; 308 309connection.disableAirplaneMode((error: BusinessError) => { 310 console.log(JSON.stringify(error)); 311}); 312``` 313 314## connection.disableAirplaneMode 315 316disableAirplaneMode(): Promise\<void> 317 318关闭飞行模式,使用Promise方式作为异步方法。 319 320**系统接口**:此接口为系统接口。 321 322**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 323 324**系统能力**:SystemCapability.Communication.NetManager.Core 325 326**返回值:** 327 328| 类型 | 说明 | 329| ------------------------------------------- | ----------------------------- | 330| Promise\<void> | 无返回值的Promise对象。 | 331 332**错误码:** 333 334| 错误码ID | 错误信息 | 335| ------- | ----------------------------- | 336| 201 | Permission denied. | 337| 202 | Non-system applications use system APIs. | 338| 401 | Parameter error. | 339| 2100002 | Operation failed. Cannot connect to service.| 340| 2100003 | System internal error. | 341 342**示例:** 343 344```ts 345import connection from '@ohos.net.connection'; 346 347connection.disableAirplaneMode().then((error: void) => { 348 console.log(JSON.stringify(error)); 349}); 350``` 351 352 353## connection.factoryReset<sup>11+</sup> 354 355factoryReset(): Promise\<void\> 356 357出厂重置网络设置,使用Promise方式作为异步方法。 358 359**系统接口**:此接口为系统接口。 360 361**需要权限**:ohos.permission.CONNECTIVITY_INTERNAL 362 363**系统能力**:SystemCapability.Communication.NetManager.Core 364 365**返回值:** 366 367| 类型 | 说明 | 368| ---------------------- | ----------------------- | 369| Promise\<void\> | 无返回值的Promise对象。 | 370 371**错误码:** 372 373| 错误码ID | 错误信息 | 374| ------- | ------------------------------------------ | 375| 201 | Permission denied. | 376| 202 | Non-system applications use system APIs. | 377| 401 | Parameter error. | 378| 2100002 | Operation failed. Cannot connect to service.| 379| 2100003 | System internal error. | 380 381**示例:** 382 383```ts 384import connection from '@ohos.net.connection'; 385import { BusinessError } from '@ohos.base'; 386connection.factoryReset().then(() => { 387 console.log("success"); 388}).catch((error: BusinessError) => { 389 console.log(JSON.stringify(error)); 390}) 391```