1# @ohos.net.http (数据请求) 2 3本模块提供HTTP数据请求能力。应用可以通过HTTP发起一个数据请求,支持常见的GET、POST、OPTIONS、HEAD、PUT、DELETE、TRACE、CONNECT方法。 4 5> **说明:** 6> 7>本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8> 9 10## 导入模块 11 12```ts 13import http from '@ohos.net.http'; 14``` 15 16## 完整示例 17 18```ts 19// 引入包名 20import http from '@ohos.net.http'; 21import { BusinessError } from '@ohos.base'; 22 23// 每一个httpRequest对应一个HTTP请求任务,不可复用 24let httpRequest = http.createHttp(); 25// 用于订阅HTTP响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息 26// 从API 8开始,使用on('headersReceive', Callback)替代on('headerReceive', AsyncCallback)。 8+ 27httpRequest.on('headersReceive', (header:Object) => { 28 console.info('header: ' + JSON.stringify(header)); 29}); 30class ExtraData { 31 public data: string; 32 33 constructor(data: string) { 34 this.data = data; 35 } 36} 37class Header { 38 public contentType: string; 39 40 constructor(contentType: string) { 41 this.contentType = contentType; 42 } 43} 44httpRequest.request( 45 // 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定 46 "EXAMPLE_URL", 47 { 48 method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET 49 // 开发者根据自身业务需要添加header字段 50 header: new Header('application/json'), 51 // 当使用POST请求时此字段用于传递内容 52 extraData: new ExtraData('data to send'), 53 expectDataType: http.HttpDataType.STRING, // 可选,指定返回数据的类型 54 usingCache: true, // 可选,默认为true 55 priority: 1, // 可选,默认为1 56 connectTimeout: 60000, // 可选,默认为60000ms 57 readTimeout: 60000, // 可选,默认为60000ms 58 usingProtocol: http.HttpProtocol.HTTP1_1, // 可选,协议类型默认值由系统自动指定 59 usingProxy: false, //可选,默认不使用网络代理,自API 10开始支持该属性 60 caPath: "", // 可选,默认使用系统预设CA证书,自API 10开始支持该属性 61 }, 62 (err: BusinessError, data: http.HttpResponse ) => { 63 if (!err) { 64 // data.result为HTTP响应内容,可根据业务需要进行解析 65 console.info('Result:' + JSON.stringify(data.result)); 66 console.info('code:' + JSON.stringify(data.responseCode)); 67 // data.header为HTTP响应头,可根据业务需要进行解析 68 console.info('header:' + JSON.stringify(data.header)); 69 console.info('cookies:' + JSON.stringify(data.cookies)); // 8+ 70 // 取消订阅HTTP响应头事件 71 httpRequest.off('headersReceive'); 72 // 当该请求使用完毕时,开发者务必调用destroy方法主动销毁该JavaScript Object。 73 httpRequest.destroy(); 74 } else { 75 console.info('error:' + JSON.stringify(err)); 76 // 取消订阅HTTP响应头事件 77 httpRequest.off('headersReceive'); 78 // 当该请求使用完毕时,开发者务必调用destroy方法主动销毁该JavaScript Object。 79 httpRequest.destroy(); 80 } 81); 82``` 83 84> **说明:** 85> console.info()输出的数据中包含换行符会导致数据出现截断现象。 86 87## http.createHttp<sup>6+</sup> 88 89createHttp(): HttpRequest 90 91创建一个HTTP请求,里面包括发起请求、中断请求、订阅/取消订阅HTTP Response Header事件。每一个HttpRequest对象对应一个HTTP请求。如需发起多个HTTP请求,须为每个HTTP请求创建对应HttpRequest对象。 92 93> **说明:** 94> 当该请求使用完毕时,须调用destroy方法主动销毁HttpRequest对象。 95 96**系统能力**:SystemCapability.Communication.NetStack 97 98**返回值:** 99 100| 类型 | 说明 | 101| :---------- | :----------------------------------------------------------- | 102| HttpRequest | 返回一个HttpRequest对象,里面包括request、requestInStream、destroy、on和off方法。 | 103 104**示例:** 105 106```ts 107import http from '@ohos.net.http'; 108 109let httpRequest = http.createHttp(); 110``` 111 112## HttpRequest 113 114HTTP请求任务。在调用HttpRequest的方法前,需要先通过[createHttp()](#httpcreatehttp)创建一个任务。 115 116### request<sup>6+</sup> 117 118request(url: string, callback: AsyncCallback\<HttpResponse\>):void 119 120根据URL地址,发起HTTP网络请求,使用callback方式作为异步方法。 121 122> **说明:** 123> 此接口仅支持数据大小为5M以内的数据接收。 124 125**需要权限**:ohos.permission.INTERNET 126 127**系统能力**:SystemCapability.Communication.NetStack 128 129**参数:** 130 131| 参数名 | 类型 | 必填 | 说明 | 132| -------- | ---------------------------------------------- | ---- | ----------------------- | 133| url | string | 是 | 发起网络请求的URL地址。 | 134| callback | AsyncCallback\<[HttpResponse](#httpresponse)\> | 是 | 回调函数。 | 135 136**错误码:** 137 138| 错误码ID | 错误信息 | 139|---------|-------------------------------------------------------| 140| 401 | Parameter error. | 141| 201 | Permission denied. | 142| 2300001 | Unsupported protocol. | 143| 2300003 | URL using bad/illegal format or missing URL. | 144| 2300005 | Couldn't resolve proxy name. | 145| 2300006 | Couldn't resolve host name. | 146| 2300007 | Couldn't connect to server. | 147| 2300008 | Weird server reply. | 148| 2300009 | Access denied to remote resource. | 149| 2300016 | Error in the HTTP2 framing layer. | 150| 2300018 | Transferred a partial file. | 151| 2300023 | Failed writing received data to disk/application. | 152| 2300025 | Upload failed. | 153| 2300026 | Failed to open/read local data from file/application. | 154| 2300027 | Out of memory. | 155| 2300028 | Timeout was reached. | 156| 2300047 | Number of redirects hit maximum amount. | 157| 2300052 | Server returned nothing (no headers, no data). | 158| 2300055 | Failed sending data to the peer. | 159| 2300056 | Failure when receiving data from the peer. | 160| 2300058 | Problem with the local SSL certificate. | 161| 2300059 | Couldn't use specified SSL cipher. | 162| 2300060 | SSL peer certificate or SSH remote key was not OK. | 163| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.| 164| 2300063 | Maximum file size exceeded. | 165| 2300070 | Disk full or allocation exceeded. | 166| 2300073 | Remote file already exists. | 167| 2300077 | Problem with the SSL CA cert (path? access rights?). | 168| 2300078 | Remote file not found. | 169| 2300094 | An authentication function returned an error. | 170| 2300999 | Unknown Other Error. | 171 172> **错误码说明:** 173> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。 174> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html) 175 176**示例:** 177 178```ts 179import http from '@ohos.net.http'; 180 181let httpRequest = http.createHttp(); 182httpRequest.request("EXAMPLE_URL", (err: Error, data: http.HttpResponse) => { 183 if (!err) { 184 console.info('Result:' + data.result); 185 console.info('code:' + data.responseCode); 186 console.info('header:' + JSON.stringify(data.header)); 187 console.info('cookies:' + data.cookies); // 8+ 188 } else { 189 console.info('error:' + JSON.stringify(err)); 190 } 191}); 192``` 193 194### request<sup>6+</sup> 195 196request(url: string, options: HttpRequestOptions, callback: AsyncCallback\<HttpResponse\>):void 197 198根据URL地址和相关配置项,发起HTTP网络请求,使用callback方式作为异步方法。 199 200> **说明:** 201> 此接口仅支持数据大小为5M以内的数据接收。 202 203**需要权限**:ohos.permission.INTERNET 204 205**系统能力**:SystemCapability.Communication.NetStack 206 207**参数:** 208 209| 参数名 | 类型 | 必填 | 说明 | 210| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- | 211| url | string | 是 | 发起网络请求的URL地址。 | 212| options | HttpRequestOptions | 是 | 参考[HttpRequestOptions](#httprequestoptions)。 | 213| callback | AsyncCallback\<[HttpResponse](#httpresponse)\> | 是 | 回调函数。 | 214 215**错误码:** 216 217| 错误码ID | 错误信息 | 218|---------|-------------------------------------------------------| 219| 401 | Parameter error. | 220| 201 | Permission denied. | 221| 2300001 | Unsupported protocol. | 222| 2300003 | URL using bad/illegal format or missing URL. | 223| 2300005 | Couldn't resolve proxy name. | 224| 2300006 | Couldn't resolve host name. | 225| 2300007 | Couldn't connect to server. | 226| 2300008 | Weird server reply. | 227| 2300009 | Access denied to remote resource. | 228| 2300016 | Error in the HTTP2 framing layer. | 229| 2300018 | Transferred a partial file. | 230| 2300023 | Failed writing received data to disk/application. | 231| 2300025 | Upload failed. | 232| 2300026 | Failed to open/read local data from file/application. | 233| 2300027 | Out of memory. | 234| 2300028 | Timeout was reached. | 235| 2300047 | Number of redirects hit maximum amount. | 236| 2300052 | Server returned nothing (no headers, no data). | 237| 2300055 | Failed sending data to the peer. | 238| 2300056 | Failure when receiving data from the peer. | 239| 2300058 | Problem with the local SSL certificate. | 240| 2300059 | Couldn't use specified SSL cipher. | 241| 2300060 | SSL peer certificate or SSH remote key was not OK. | 242| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.| 243| 2300063 | Maximum file size exceeded. | 244| 2300070 | Disk full or allocation exceeded. | 245| 2300073 | Remote file already exists. | 246| 2300077 | Problem with the SSL CA cert (path? access rights?). | 247| 2300078 | Remote file not found. | 248| 2300094 | An authentication function returned an error. | 249| 2300999 | Unknown Other Error. | 250 251> **错误码说明:** 252> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。 253> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html) 254 255**示例:** 256 257```ts 258import http from '@ohos.net.http'; 259 260class Header { 261 public contentType: string; 262 263 constructor(contentType: string) { 264 this.contentType = contentType; 265 } 266} 267 268let httpRequest = http.createHttp(); 269let promise = httpRequest.request("EXAMPLE_URL", { 270 method: http.RequestMethod.GET, 271 connectTimeout: 60000, 272 readTimeout: 60000, 273 header: new Header('application/json') 274}); 275 276promise.then((data:http.HttpResponse) => { 277 console.info('Result:' + data.result); 278 console.info('code:' + data.responseCode); 279 console.info('header:' + JSON.stringify(data.header)); 280 console.info('cookies:' + data.cookies); // 8+ 281 console.info('header.Content-Type:' + data.header); 282 console.info('header.Status-Line:' + data.header); 283 284}).catch((err:Error) => { 285 console.info('error:' + JSON.stringify(err)); 286}); 287``` 288 289### request<sup>6+</sup> 290 291request(url: string, options? : HttpRequestOptions): Promise\<HttpResponse\> 292 293根据URL地址,发起HTTP网络请求,使用Promise方式作为异步方法。 294 295> **说明:** 296> 此接口仅支持数据大小为5M以内的数据接收。 297 298**需要权限**:ohos.permission.INTERNET 299 300**系统能力**:SystemCapability.Communication.NetStack 301 302**参数:** 303 304| 参数名 | 类型 | 必填 | 说明 | 305| ------- | ------------------ | ---- | ----------------------------------------------- | 306| url | string | 是 | 发起网络请求的URL地址。 | 307| options | HttpRequestOptions | 否 | 参考[HttpRequestOptions](#httprequestoptions)。 | 308 309**返回值:** 310 311| 类型 | 说明 | 312| :------------------------------------- | :-------------------------------- | 313| Promise<[HttpResponse](#httpresponse)> | 以Promise形式返回发起请求的结果。 | 314 315**错误码:** 316 317| 错误码ID | 错误信息 | 318|---------|-------------------------------------------------------| 319| 401 | Parameter error. | 320| 201 | Permission denied. | 321| 2300001 | Unsupported protocol. | 322| 2300003 | URL using bad/illegal format or missing URL. | 323| 2300005 | Couldn't resolve proxy name. | 324| 2300006 | Couldn't resolve host name. | 325| 2300007 | Couldn't connect to server. | 326| 2300008 | Weird server reply. | 327| 2300009 | Access denied to remote resource. | 328| 2300016 | Error in the HTTP2 framing layer. | 329| 2300018 | Transferred a partial file. | 330| 2300023 | Failed writing received data to disk/application. | 331| 2300025 | Upload failed. | 332| 2300026 | Failed to open/read local data from file/application. | 333| 2300027 | Out of memory. | 334| 2300028 | Timeout was reached. | 335| 2300047 | Number of redirects hit maximum amount. | 336| 2300052 | Server returned nothing (no headers, no data). | 337| 2300055 | Failed sending data to the peer. | 338| 2300056 | Failure when receiving data from the peer. | 339| 2300058 | Problem with the local SSL certificate. | 340| 2300059 | Couldn't use specified SSL cipher. | 341| 2300060 | SSL peer certificate or SSH remote key was not OK. | 342| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.| 343| 2300063 | Maximum file size exceeded. | 344| 2300070 | Disk full or allocation exceeded. | 345| 2300073 | Remote file already exists. | 346| 2300077 | Problem with the SSL CA cert (path? access rights?). | 347| 2300078 | Remote file not found. | 348| 2300094 | An authentication function returned an error. | 349| 2300999 | Unknown Other Error. | 350 351> **错误码说明:** 352> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。 353> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html) 354 355**示例:** 356 357```ts 358import http from '@ohos.net.http'; 359 360class Header { 361 public contentType: string; 362 363 constructor(contentType: string) { 364 this.contentType = contentType; 365 } 366} 367 368let httpRequest = http.createHttp(); 369let promise = httpRequest.request("EXAMPLE_URL", { 370 method: http.RequestMethod.GET, 371 connectTimeout: 60000, 372 readTimeout: 60000, 373 header: new Header('application/json') 374}); 375promise.then((data:http.HttpResponse) => { 376 console.info('Result:' + data.result); 377 console.info('code:' + data.responseCode); 378 console.info('header:' + JSON.stringify(data.header)); 379 console.info('cookies:' + data.cookies); // 8+ 380 console.info('header.Content-Type:' + data.header); 381 console.info('header.Status-Line:' + data.header); 382}).catch((err:Error) => { 383 console.info('error:' + JSON.stringify(err)); 384}); 385``` 386 387### destroy 388 389destroy(): void 390 391中断请求任务。 392 393**系统能力**:SystemCapability.Communication.NetStack 394 395**示例:** 396 397```ts 398import http from '@ohos.net.http'; 399let httpRequest = http.createHttp(); 400 401httpRequest.destroy(); 402``` 403 404### requestInStream<sup>10+</sup> 405 406requestInStream(url: string, callback: AsyncCallback\<number\>): void 407 408根据URL地址,发起HTTP网络请求并返回流式响应,使用callback方式作为异步方法。 409 410**需要权限**:ohos.permission.INTERNET 411 412**系统能力**:SystemCapability.Communication.NetStack 413 414**参数:** 415 416| 参数名 | 类型 | 必填 | 说明 | 417| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- | 418| url | string | 是 | 发起网络请求的URL地址。 | 419| callback | AsyncCallback\<[number](#responsecode)\> | 是 | 回调函数。 | 420 421**错误码:** 422 423| 错误码ID | 错误信息 | 424|---------|-------------------------------------------------------| 425| 401 | Parameter error. | 426| 201 | Permission denied. | 427| 2300001 | Unsupported protocol. | 428| 2300003 | URL using bad/illegal format or missing URL. | 429| 2300005 | Couldn't resolve proxy name. | 430| 2300006 | Couldn't resolve host name. | 431| 2300007 | Couldn't connect to server. | 432| 2300008 | Weird server reply. | 433| 2300009 | Access denied to remote resource. | 434| 2300016 | Error in the HTTP2 framing layer. | 435| 2300018 | Transferred a partial file. | 436| 2300023 | Failed writing received data to disk/application. | 437| 2300025 | Upload failed. | 438| 2300026 | Failed to open/read local data from file/application. | 439| 2300027 | Out of memory. | 440| 2300028 | Timeout was reached. | 441| 2300047 | Number of redirects hit maximum amount. | 442| 2300052 | Server returned nothing (no headers, no data). | 443| 2300055 | Failed sending data to the peer. | 444| 2300056 | Failure when receiving data from the peer. | 445| 2300058 | Problem with the local SSL certificate. | 446| 2300059 | Couldn't use specified SSL cipher. | 447| 2300060 | SSL peer certificate or SSH remote key was not OK. | 448| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.| 449| 2300063 | Maximum file size exceeded. | 450| 2300070 | Disk full or allocation exceeded. | 451| 2300073 | Remote file already exists. | 452| 2300077 | Problem with the SSL CA cert (path? access rights?). | 453| 2300078 | Remote file not found. | 454| 2300094 | An authentication function returned an error. | 455| 2300999 | Unknown Other Error. | 456 457> **错误码说明:** 458> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。 459> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html) 460 461**示例:** 462 463```ts 464import http from '@ohos.net.http'; 465import { BusinessError } from '@ohos.base'; 466 467let httpRequest = http.createHttp(); 468httpRequest.requestInStream("EXAMPLE_URL", (err: BusinessError, data: number) => { 469 if (!err) { 470 console.info("requestInStream OK! ResponseCode is " + JSON.stringify(data)); 471 } else { 472 console.info("requestInStream ERROR : err = " + JSON.stringify(err)); 473 } 474}) 475``` 476 477### requestInStream<sup>10+</sup> 478 479requestInStream(url: string, options: HttpRequestOptions, callback: AsyncCallback\<number\>): void 480 481根据URL地址和相关配置项,发起HTTP网络请求并返回流式响应,使用callback方式作为异步方法。 482 483**需要权限**:ohos.permission.INTERNET 484 485**系统能力**:SystemCapability.Communication.NetStack 486 487**参数:** 488 489| 参数名 | 类型 | 必填 | 说明 | 490| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- | 491| url | string | 是 | 发起网络请求的URL地址。 | 492| options | HttpRequestOptions | 是 | 参考[HttpRequestOptions](#httprequestoptions)。 | 493| callback | AsyncCallback\<[number](#responsecode)\> | 是 | 回调函数。 | 494 495**错误码:** 496 497| 错误码ID | 错误信息 | 498|---------|-------------------------------------------------------| 499| 401 | Parameter error. | 500| 201 | Permission denied. | 501| 2300001 | Unsupported protocol. | 502| 2300003 | URL using bad/illegal format or missing URL. | 503| 2300005 | Couldn't resolve proxy name. | 504| 2300006 | Couldn't resolve host name. | 505| 2300007 | Couldn't connect to server. | 506| 2300008 | Weird server reply. | 507| 2300009 | Access denied to remote resource. | 508| 2300016 | Error in the HTTP2 framing layer. | 509| 2300018 | Transferred a partial file. | 510| 2300023 | Failed writing received data to disk/application. | 511| 2300025 | Upload failed. | 512| 2300026 | Failed to open/read local data from file/application. | 513| 2300027 | Out of memory. | 514| 2300028 | Timeout was reached. | 515| 2300047 | Number of redirects hit maximum amount. | 516| 2300052 | Server returned nothing (no headers, no data). | 517| 2300055 | Failed sending data to the peer. | 518| 2300056 | Failure when receiving data from the peer. | 519| 2300058 | Problem with the local SSL certificate. | 520| 2300059 | Couldn't use specified SSL cipher. | 521| 2300060 | SSL peer certificate or SSH remote key was not OK. | 522| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.| 523| 2300063 | Maximum file size exceeded. | 524| 2300070 | Disk full or allocation exceeded. | 525| 2300073 | Remote file already exists. | 526| 2300077 | Problem with the SSL CA cert (path? access rights?). | 527| 2300078 | Remote file not found. | 528| 2300094 | An authentication function returned an error. | 529| 2300999 | Unknown Other Error. | 530 531> **错误码说明:** 532> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。 533> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html) 534 535**示例:** 536 537```ts 538import http from '@ohos.net.http'; 539import { BusinessError } from '@ohos.base'; 540 541class Header { 542 public contentType: string; 543 544 constructor(contentType: string) { 545 this.contentType = contentType; 546 } 547} 548 549let httpRequest = http.createHttp(); 550httpRequest.requestInStream("EXAMPLE_URL", (err: BusinessError<void> , data: number) => { 551 if (!err) { 552 console.info("requestInStream OK! ResponseCode is " + JSON.stringify(data)); 553 } else { 554 console.info("requestInStream ERROR : err = " + JSON.stringify(err)); 555 } 556}) 557``` 558 559### requestInStream<sup>10+</sup> 560 561requestInStream(url: string, options? : HttpRequestOptions): Promise\<number\> 562 563根据URL地址,发起HTTP网络请求并返回流式响应,使用Promise方式作为异步方法。 564 565**需要权限**:ohos.permission.INTERNET 566 567**系统能力**:SystemCapability.Communication.NetStack 568 569**参数:** 570 571| 参数名 | 类型 | 必填 | 说明 | 572| ------- | ------------------ | ---- | ----------------------------------------------- | 573| url | string | 是 | 发起网络请求的URL地址。 | 574| options | HttpRequestOptions | 否 | 参考[HttpRequestOptions](#httprequestoptions)。 | 575 576**返回值:** 577 578| 类型 | 说明 | 579| :------------------------------------- | :-------------------------------- | 580| Promise\<[number](#responsecode)\> | 以Promise形式返回发起请求的结果。 | 581 582**错误码:** 583 584| 错误码ID | 错误信息 | 585|---------|-------------------------------------------------------| 586| 401 | Parameter error. | 587| 201 | Permission denied. | 588| 2300001 | Unsupported protocol. | 589| 2300003 | URL using bad/illegal format or missing URL. | 590| 2300005 | Couldn't resolve proxy name. | 591| 2300006 | Couldn't resolve host name. | 592| 2300007 | Couldn't connect to server. | 593| 2300008 | Weird server reply. | 594| 2300009 | Access denied to remote resource. | 595| 2300016 | Error in the HTTP2 framing layer. | 596| 2300018 | Transferred a partial file. | 597| 2300023 | Failed writing received data to disk/application. | 598| 2300025 | Upload failed. | 599| 2300026 | Failed to open/read local data from file/application. | 600| 2300027 | Out of memory. | 601| 2300028 | Timeout was reached. | 602| 2300047 | Number of redirects hit maximum amount. | 603| 2300052 | Server returned nothing (no headers, no data). | 604| 2300055 | Failed sending data to the peer. | 605| 2300056 | Failure when receiving data from the peer. | 606| 2300058 | Problem with the local SSL certificate. | 607| 2300059 | Couldn't use specified SSL cipher. | 608| 2300060 | SSL peer certificate or SSH remote key was not OK. | 609| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.| 610| 2300063 | Maximum file size exceeded. | 611| 2300070 | Disk full or allocation exceeded. | 612| 2300073 | Remote file already exists. | 613| 2300077 | Problem with the SSL CA cert (path? access rights?). | 614| 2300078 | Remote file not found. | 615| 2300094 | An authentication function returned an error. | 616| 2300999 | Unknown Other Error. | 617 618> **错误码说明:** 619> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。 620> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考: 621 622**示例:** 623 624```ts 625import http from '@ohos.net.http'; 626 627class Header { 628 public contentType: string; 629 630 constructor(contentType: string) { 631 this.contentType = contentType; 632 } 633} 634 635let httpRequest = http.createHttp(); 636let promise = httpRequest.request("EXAMPLE_URL", { 637 method: http.RequestMethod.GET, 638 connectTimeout: 60000, 639 readTimeout: 60000, 640 header: new Header('application/json') 641}); 642promise.then((data: http.HttpResponse) => { 643 console.info("requestInStream OK!" + JSON.stringify(data)); 644}).catch((err: Error) => { 645 console.info("requestInStream ERROR : err = " + JSON.stringify(err)); 646}); 647``` 648 649### on('headerReceive')<sup>(deprecated)</sup> 650 651on(type: 'headerReceive', callback: AsyncCallback\<Object\>): void 652 653订阅HTTP Response Header 事件。 654 655> **说明:** 656> 此接口已废弃,建议使用[on('headersReceive')<sup>8+</sup>](#onheadersreceive8)替代。 657 658**系统能力**:SystemCapability.Communication.NetStack 659 660**参数:** 661 662| 参数名 | 类型 | 必填 | 说明 | 663| -------- | ----------------------- | ---- | --------------------------------- | 664| type | string | 是 | 订阅的事件类型,'headerReceive'。 | 665| callback | AsyncCallback\<Object\> | 是 | 回调函数。 | 666 667**示例:** 668 669```ts 670import http from '@ohos.net.http'; 671import { BusinessError } from '@ohos.base'; 672 673let httpRequest = http.createHttp(); 674httpRequest.on('headerReceive', (data: BusinessError) => { 675 console.info('error:' + JSON.stringify(data)); 676}); 677``` 678 679### off('headerReceive')<sup>(deprecated)</sup> 680 681off(type: 'headerReceive', callback?: AsyncCallback\<Object\>): void 682 683取消订阅HTTP Response Header 事件。 684 685> **说明:** 686> 687>1. 此接口已废弃,建议使用[off('headersReceive')<sup>8+</sup>](#offheadersreceive8)替代。 688> 689>2. 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。 690 691**系统能力**:SystemCapability.Communication.NetStack 692 693**参数:** 694 695| 参数名 | 类型 | 必填 | 说明 | 696| -------- | ----------------------- | ---- | ------------------------------------- | 697| type | string | 是 | 取消订阅的事件类型,'headerReceive'。 | 698| callback | AsyncCallback\<Object\> | 否 | 回调函数。 | 699 700**示例:** 701 702```ts 703import http from '@ohos.net.http'; 704 705let httpRequest = http.createHttp(); 706httpRequest.off('headerReceive'); 707``` 708 709### on('headersReceive')<sup>8+</sup> 710 711on(type: 'headersReceive', callback: Callback\<Object\>): void 712 713订阅HTTP Response Header 事件。 714 715**系统能力**:SystemCapability.Communication.NetStack 716 717**参数:** 718 719| 参数名 | 类型 | 必填 | 说明 | 720| -------- | ------------------ | ---- | ---------------------------------- | 721| type | string | 是 | 订阅的事件类型:'headersReceive'。 | 722| callback | Callback\<Object\> | 是 | 回调函数。 | 723 724**示例:** 725 726```ts 727import http from '@ohos.net.http'; 728 729let httpRequest = http.createHttp(); 730httpRequest.on('headersReceive', (header: Object) => { 731 console.info('header: ' + JSON.stringify(header)); 732}); 733``` 734 735### off('headersReceive')<sup>8+</sup> 736 737off(type: 'headersReceive', callback?: Callback\<Object\>): void 738 739取消订阅HTTP Response Header 事件。 740 741> **说明:** 742> 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。 743 744**系统能力**:SystemCapability.Communication.NetStack 745 746**参数:** 747 748| 参数名 | 类型 | 必填 | 说明 | 749| -------- | ------------------ | ---- | -------------------------------------- | 750| type | string | 是 | 取消订阅的事件类型:'headersReceive'。 | 751| callback | Callback\<Object\> | 否 | 回调函数。 | 752 753**示例:** 754 755```ts 756import http from '@ohos.net.http'; 757 758let httpRequest = http.createHttp(); 759httpRequest.off('headersReceive'); 760``` 761 762### once('headersReceive')<sup>8+</sup> 763 764once(type: 'headersReceive', callback: Callback\<Object\>): void 765 766订阅HTTP Response Header 事件,但是只触发一次。一旦触发之后,订阅器就会被移除。使用callback方式作为异步方法。 767 768**系统能力**:SystemCapability.Communication.NetStack 769 770**参数:** 771 772| 参数名 | 类型 | 必填 | 说明 | 773| -------- | ------------------ | ---- | ---------------------------------- | 774| type | string | 是 | 订阅的事件类型:'headersReceive'。 | 775| callback | Callback\<Object\> | 是 | 回调函数。 | 776 777**示例:** 778 779```ts 780import http from '@ohos.net.http'; 781 782let httpRequest = http.createHttp(); 783httpRequest.once('headersReceive', (header: Object) => { 784 console.info('header: ' + JSON.stringify(header)); 785}); 786``` 787 788### on('dataReceive')<sup>10+</sup> 789 790on(type: 'dataReceive', callback: Callback\<ArrayBuffer\>): void 791 792订阅HTTP流式响应数据接收事件。 793 794> **说明:** 795> 暂不支持订阅HTTP流式数据上传的相关事件。 796 797**系统能力**:SystemCapability.Communication.NetStack 798 799**参数:** 800 801| 参数名 | 类型 | 必填 | 说明 | 802| -------- | ----------------------- | ---- | --------------------------------- | 803| type | string | 是 | 订阅的事件类型,'dataReceive'。 | 804| callback | AsyncCallback\<ArrayBuffer\> | 是 | 回调函数。 | 805 806**示例:** 807 808```ts 809import http from '@ohos.net.http'; 810 811let httpRequest = http.createHttp(); 812httpRequest.on('dataReceive', (data: ArrayBuffer) => { 813 console.info('dataReceive length: ' + JSON.stringify(data.byteLength)); 814}); 815``` 816 817### off('dataReceive')<sup>10+</sup> 818 819off(type: 'dataReceive', callback?: Callback\<ArrayBuffer\>): void 820 821取消订阅HTTP流式响应数据接收事件。 822 823> **说明:** 824> 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。 825 826**系统能力**:SystemCapability.Communication.NetStack 827 828**参数:** 829 830| 参数名 | 类型 | 必填 | 说明 | 831| -------- | ------------------ | ---- | -------------------------------------- | 832| type | string | 是 | 取消订阅的事件类型:'dataReceive'。 | 833| callback | Callback\<ArrayBuffer\> | 否 | 回调函数。 | 834 835**示例:** 836 837```ts 838import http from '@ohos.net.http'; 839 840let httpRequest = http.createHttp(); 841httpRequest.off('dataReceive'); 842``` 843 844### on('dataEnd')<sup>10+</sup> 845 846on(type: 'dataEnd', callback: Callback\<void\>): void 847 848订阅HTTP流式响应数据接收完毕事件。 849 850> **说明:** 851> 暂不支持订阅HTTP流式数据上传的相关事件。 852 853**系统能力**:SystemCapability.Communication.NetStack 854 855**参数:** 856 857| 参数名 | 类型 | 必填 | 说明 | 858| -------- | ----------------------- | ---- | --------------------------------- | 859| type | string | 是 | 订阅的事件类型,'dataEnd'。 | 860| callback | AsyncCallback\<void\> | 是 | 回调函数。 | 861 862**示例:** 863 864```ts 865import http from '@ohos.net.http'; 866 867let httpRequest = http.createHttp(); 868httpRequest.on('dataEnd', () => { 869 console.info('Receive dataEnd !'); 870}); 871``` 872 873### off('dataEnd')<sup>10+</sup> 874 875off(type: 'dataEnd', callback?: Callback\<void\>): void 876 877取消订阅HTTP流式响应数据接收完毕事件。 878 879> **说明:** 880> 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。 881 882**系统能力**:SystemCapability.Communication.NetStack 883 884**参数:** 885 886| 参数名 | 类型 | 必填 | 说明 | 887| -------- | ------------------ | ---- | -------------------------------------- | 888| type | string | 是 | 取消订阅的事件类型:'dataEnd'。 | 889| callback | Callback\<void\> | 否 | 回调函数。 | 890 891**示例:** 892 893```ts 894import http from '@ohos.net.http'; 895 896let httpRequest = http.createHttp(); 897httpRequest.off('dataEnd'); 898``` 899 900### on('dataReceiveProgress')<sup>10+</sup> 901 902on(type: 'dataReceiveProgress', callback: Callback\<{ receiveSize: number, totalSize: number }\>): void 903 904订阅HTTP流式响应数据接收进度事件。 905 906> **说明:** 907> 暂不支持订阅HTTP流式数据上传的相关事件。 908 909**系统能力**:SystemCapability.Communication.NetStack 910 911**参数:** 912 913| 参数名 | 类型 | 必填 | 说明 | 914| -------- | ----------------------- | ---- | --------------------------------- | 915| type | string | 是 | 订阅的事件类型,'dataReceiveProgress'。 | 916| callback | AsyncCallback\<{ receiveSize: number, totalSize: number }\> | 是 | 回调函数。<br>receiveSize:已接收的数据字节数,totalSize待接收的字节总数 | 917 918**示例:** 919 920```ts 921import http from '@ohos.net.http'; 922 923class RequestData{ 924 receiveSize: number = 2000 925 totalSize: number = 2000 926} 927 928let httpRequest = http.createHttp(); 929httpRequest.on('dataReceiveProgress', (data: RequestData) => { 930 console.info('dataReceiveProgress:' + JSON.stringify(data)); 931}); 932``` 933 934### off('dataReceiveProgress')<sup>10+</sup> 935 936off(type: 'dataReceiveProgress', callback?: Callback\<{ receiveSize: number, totalSize: number }\>): void 937 938取消订阅HTTP流式响应数据接收进度事件。 939 940> **说明:** 941> 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。 942 943**系统能力**:SystemCapability.Communication.NetStack 944 945**参数:** 946 947| 参数名 | 类型 | 必填 | 说明 | 948| -------- | ------------------ | ---- | -------------------------------------- | 949| type | string | 是 | 取消订阅的事件类型:'dataReceiveProgress'。 | 950| callback | Callback\<{ receiveSize: number, totalSize: number }\> | 否 | 回调函数。 | 951 952**示例:** 953 954```ts 955import http from '@ohos.net.http'; 956 957let httpRequest = http.createHttp(); 958httpRequest.off('dataReceiveProgress'); 959``` 960 961## HttpRequestOptions<sup>6+</sup> 962 963发起请求可选参数的类型和取值范围。 964 965**系统能力**:SystemCapability.Communication.NetStack 966 967| 名称 | 类型 | 必填 | 说明 | 968| -------------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 969| method | [RequestMethod](#requestmethod) | 否 | 请求方式,默认为GET。 | 970| extraData | string<sup>6+</sup> \| Object<sup>6+</sup> \| ArrayBuffer<sup>8+</sup> | 否 | 发送请求的额外数据,默认无此字段。<br />当HTTP请求为POST、PUT等方法时,此字段为HTTP请求的content,以UTF-8编码形式作为请求体。当'Content-Type'为'application/x-www-form-urlencoded'时,请求提交的信息主体数据应在key和value进行URL转码后按照键值对"key1=value1&key2=value2&key3=value3"的方式进行编码,该字段对应的类型通常为String;当'Content-Type'为'text/xml'时,该字段对应的类型通常为String;当'Content-Type'为'application/json'时,该字段对应的类型通常为Object;当'Content-Type'为'application/octet-stream'时,该字段对应的类型通常为ArrayBuffer;当'Content-Type'为'multipart/form-data'且需上传的字段为文件时,该字段对应的类型通常为ArrayBuffer。以上信息仅供参考,并可能根据具体情况有所不同。<br />- 当HTTP请求为GET、OPTIONS、DELETE、TRACE、CONNECT等方法时,此字段为HTTP请求参数的补充。开发者需传入Encode编码后的string类型参数,Object类型的参数无需预编码,参数内容会拼接到URL中进行发送;ArrayBuffer类型的参数不会做拼接处理。 | 971| <span name="expectDataType">[expectDataType<sup>9+</sup>](#result)</span> | [HttpDataType](#httpdatatype9) | 否 | 指定返回数据的类型,默认无此字段。如果设置了此参数,系统将优先返回指定的类型。 | 972| usingCache<sup>9+</sup> | boolean | 否 | 是否使用缓存,默认为true。 | 973| priority<sup>9+</sup> | number | 否 | 优先级,范围[1,1000],默认是1。 | 974| header | Object | 否 | HTTP请求头字段。默认{'Content-Type': 'application/json'}。 | 975| readTimeout | number | 否 | 读取超时时间。单位为毫秒(ms),默认为60000ms。<br />设置为0表示不会出现超时情况。 | 976| connectTimeout | number | 否 | 连接超时时间。单位为毫秒(ms),默认为60000ms。 | 977| usingProtocol<sup>9+</sup> | [HttpProtocol](#httpprotocol9) | 否 | 使用协议。默认值由系统自动指定。 | 978| usingProxy<sup>10+</sup> | boolean \| Object | 否 | 是否使用HTTP代理,默认为false,不使用代理。<br />- 当usingProxy为布尔类型true时,使用默认网络代理。<br />- 当usingProxy为object类型时,使用指定网络代理。 | 979| caPath<sup>10+</sup> | string | 否 | 如果设置了此参数,系统将使用用户指定路径的CA证书,(开发者需保证该路径下CA证书的可访问性),否则将使用系统预设CA证书,系统预设CA证书位置:/etc/ssl/certs/cacert.pem。证书路径为沙箱映射路径(开发者可通过Global.getContext().filesDir获取应用沙箱路径)。目前仅支持后缀名为.pem的文本格式证书。 | 980 981## RequestMethod<sup>6+</sup> 982 983HTTP 请求方法。 984 985**系统能力**:SystemCapability.Communication.NetStack 986 987| 名称 | 值 | 说明 | 988| :------ | ------- | :------------------ | 989| OPTIONS | "OPTIONS" | HTTP 请求 OPTIONS。 | 990| GET | "GET" | HTTP 请求 GET。 | 991| HEAD | "HEAD" | HTTP 请求 HEAD。 | 992| POST | "POST" | HTTP 请求 POST。 | 993| PUT | "PUT" | HTTP 请求 PUT。 | 994| DELETE | "DELETE" | HTTP 请求 DELETE。 | 995| TRACE | "TRACE" | HTTP 请求 TRACE。 | 996| CONNECT | "CONNECT" | HTTP 请求 CONNECT。 | 997 998## ResponseCode<sup>6+</sup> 999 1000发起请求返回的响应码。 1001 1002**系统能力**:SystemCapability.Communication.NetStack 1003 1004| 名称 | 值 | 说明 | 1005| ----------------- | ---- | ------------------------------------------------------------ | 1006| OK | 200 | 请求成功。一般用于GET与POST请求。 | 1007| CREATED | 201 | 已创建。成功请求并创建了新的资源。 | 1008| ACCEPTED | 202 | 已接受。已经接受请求,但未处理完成。 | 1009| NOT_AUTHORITATIVE | 203 | 非授权信息。请求成功。 | 1010| NO_CONTENT | 204 | 无内容。服务器成功处理,但未返回内容。 | 1011| RESET | 205 | 重置内容。 | 1012| PARTIAL | 206 | 部分内容。服务器成功处理了部分GET请求。 | 1013| MULT_CHOICE | 300 | 多种选择。 | 1014| MOVED_PERM | 301 | 永久移动。请求的资源已被永久的移动到新URI,返回信息会包括新的URI,浏览器会自动定向到新URI。 | 1015| MOVED_TEMP | 302 | 临时移动。 | 1016| SEE_OTHER | 303 | 查看其它地址。 | 1017| NOT_MODIFIED | 304 | 未修改。 | 1018| USE_PROXY | 305 | 使用代理。 | 1019| BAD_REQUEST | 400 | 客户端请求的语法错误,服务器无法理解。 | 1020| UNAUTHORIZED | 401 | 请求要求用户的身份认证。 | 1021| PAYMENT_REQUIRED | 402 | 保留,将来使用。 | 1022| FORBIDDEN | 403 | 服务器理解请求客户端的请求,但是拒绝执行此请求。 | 1023| NOT_FOUND | 404 | 服务器无法根据客户端的请求找到资源(网页)。 | 1024| BAD_METHOD | 405 | 客户端请求中的方法被禁止。 | 1025| NOT_ACCEPTABLE | 406 | 服务器无法根据客户端请求的内容特性完成请求。 | 1026| PROXY_AUTH | 407 | 请求要求代理的身份认证。 | 1027| CLIENT_TIMEOUT | 408 | 请求时间过长,超时。 | 1028| CONFLICT | 409 | 服务器完成客户端的PUT请求是可能返回此代码,服务器处理请求时发生了冲突。 | 1029| GONE | 410 | 客户端请求的资源已经不存在。 | 1030| LENGTH_REQUIRED | 411 | 服务器无法处理客户端发送的不带Content-Length的请求信息。 | 1031| PRECON_FAILED | 412 | 客户端请求信息的先决条件错误。 | 1032| ENTITY_TOO_LARGE | 413 | 由于请求的实体过大,服务器无法处理,因此拒绝请求。 | 1033| REQ_TOO_LONG | 414 | 请求的URI过长(URI通常为网址),服务器无法处理。 | 1034| UNSUPPORTED_TYPE | 415 | 服务器无法处理请求的格式。 | 1035| INTERNAL_ERROR | 500 | 服务器内部错误,无法完成请求。 | 1036| NOT_IMPLEMENTED | 501 | 服务器不支持请求的功能,无法完成请求。 | 1037| BAD_GATEWAY | 502 | 充当网关或代理的服务器,从远端服务器接收到了一个无效的请求。 | 1038| UNAVAILABLE | 503 | 由于超载或系统维护,服务器暂时的无法处理客户端的请求。 | 1039| GATEWAY_TIMEOUT | 504 | 充当网关或代理的服务器,未及时从远端服务器获取请求。 | 1040| VERSION | 505 | 服务器请求的HTTP协议的版本。 | 1041 1042## HttpResponse<sup>6+</sup> 1043 1044request方法回调函数的返回值类型。 1045 1046**系统能力**:SystemCapability.Communication.NetStack 1047 1048| 名称 | 类型 | 必填 | 说明 | 1049| -------------------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 1050| <span name="result">[result](#expectDataType)</span> | string<sup>6+</sup> \| Object<sup>deprecated 8+</sup> \| ArrayBuffer<sup>8+</sup> | 是 | HTTP请求根据响应头中Content-type类型返回对应的响应格式内容,若HttpRequestOptions无expectDataType字段,按如下规则返回:<br />- application/json:返回JSON格式的字符串;<br />- application/octet-stream:ArrayBuffer;<br />- image:ArrayBuffer;<br />- 其他:string。<br /> 若HttpRequestOption有expectDataType字段,开发者需传入与服务器返回类型相同的数据类型。 | 1051| resultType<sup>9+</sup> | [HttpDataType](#httpdatatype9) | 是 | 返回值类型。 | 1052| responseCode | [ResponseCode](#responsecode) \| number | 是 | 回调函数执行成功时,此字段为[ResponseCode](#responsecode)。若执行失败,错误码将会从AsyncCallback中的err字段返回。 | 1053| header | Object | 是 | 发起HTTP请求返回来的响应头。当前返回的是JSON格式字符串,如需具体字段内容,需开发者自行解析。常见字段及解析方式如下:<br/>- content-type:header['content-type'];<br />- status-line:header['status-line'];<br />- date:header.date/header['date'];<br />- server:header.server/header['server']; | 1054| cookies<sup>8+</sup> | string | 是 | 服务器返回的 cookies。 | 1055 1056## http.createHttpResponseCache<sup>9+</sup> 1057 1058createHttpResponseCache(cacheSize?: number): HttpResponseCache 1059 1060创建一个默认的对象来存储HTTP访问请求的响应。 1061 1062**系统能力**:SystemCapability.Communication.NetStack 1063 1064**参数:** 1065 1066| 参数名 | 类型 | 必填 | 说明 | 1067| -------- | --------------------------------------- | ---- | ---------- | 1068| cacheSize | number | 否 | 缓存大小最大为10\*1024\*1024(10MB),默认最大。 | 1069 1070**返回值:** 1071 1072| 类型 | 说明 | 1073| :---------- | :----------------------------------------------------------- | 1074| [HttpResponseCache](#httpresponsecache9) | 返回一个存储HTTP访问请求响应的对象。 | 1075 1076**示例:** 1077 1078```ts 1079import http from '@ohos.net.http'; 1080 1081let httpResponseCache = http.createHttpResponseCache(); 1082``` 1083 1084## HttpResponseCache<sup>9+</sup> 1085 1086存储HTTP访问请求响应的对象。在调用HttpResponseCache的方法前,需要先通过[createHttpResponseCache()](#httpcreatehttpresponsecache9)创建一个任务。 1087 1088### flush<sup>9+</sup> 1089 1090flush(callback: AsyncCallback\<void\>): void 1091 1092将缓存中的数据写入文件系统,以便在下一个HTTP请求中访问所有缓存数据,使用callback方式作为异步方法。缓存数据包括:响应头(header)、响应体(result)、cookies、请求时间(requestTime)和响应时间(responseTime)。 1093 1094**系统能力**:SystemCapability.Communication.NetStack 1095 1096**参数:** 1097 1098| 参数名 | 类型 | 必填 | 说明 | 1099| -------- | --------------------------------------- | ---- | ---------- | 1100| callback | AsyncCallback\<void\> | 是 | 回调函数返回写入结果。 | 1101 1102**示例:** 1103 1104```ts 1105import http from '@ohos.net.http'; 1106import { BusinessError } from '@ohos.base'; 1107 1108let httpResponseCache = http.createHttpResponseCache(); 1109httpResponseCache.flush((err: BusinessError) => { 1110 if (err) { 1111 console.info('flush fail'); 1112 return; 1113 } 1114 console.info('flush success'); 1115}); 1116``` 1117 1118### flush<sup>9+</sup> 1119 1120flush(): Promise\<void\> 1121 1122将缓存中的数据写入文件系统,以便在下一个HTTP请求中访问所有缓存数据,使用Promise方式作为异步方法。 1123 1124**系统能力**:SystemCapability.Communication.NetStack 1125 1126**返回值:** 1127 1128| 类型 | 说明 | 1129| --------------------------------- | ------------------------------------- | 1130| Promise\<void\> | 以Promise形式返回写入结果。 | 1131 1132**示例:** 1133 1134```ts 1135import http from '@ohos.net.http'; 1136import { BusinessError } from '@ohos.base'; 1137 1138let httpResponseCache = http.createHttpResponseCache(); 1139httpResponseCache.flush().then(() => { 1140 console.info('flush success'); 1141}).catch((err: BusinessError) => { 1142 console.info('flush fail'); 1143}); 1144``` 1145 1146### delete<sup>9+</sup> 1147 1148delete(callback: AsyncCallback\<void\>): void 1149 1150禁用缓存并删除其中的数据,使用callback方式作为异步方法。 1151 1152**系统能力**:SystemCapability.Communication.NetStack 1153 1154**参数:** 1155 1156| 参数名 | 类型 | 必填 | 说明 | 1157| -------- | --------------------------------------- | ---- | ---------- | 1158| callback | AsyncCallback\<void\> | 是 | 回调函数返回删除结果。| 1159 1160**示例:** 1161 1162```ts 1163import http from '@ohos.net.http'; 1164import { BusinessError } from '@ohos.base'; 1165 1166let httpResponseCache = http.createHttpResponseCache(); 1167httpResponseCache.delete((err: BusinessError) => { 1168 if (err) { 1169 console.info('delete fail'); 1170 return; 1171 } 1172 console.info('delete success'); 1173}); 1174``` 1175 1176### delete<sup>9+</sup> 1177 1178delete(): Promise\<void\> 1179 1180禁用缓存并删除其中的数据,使用Promise方式作为异步方法。 1181 1182**系统能力**:SystemCapability.Communication.NetStack 1183 1184**返回值:** 1185 1186| 类型 | 说明 | 1187| --------------------------------- | ------------------------------------- | 1188| Promise\<void\> | 以Promise形式返回删除结果。 | 1189 1190**示例:** 1191 1192```ts 1193import http from '@ohos.net.http'; 1194import { BusinessError } from '@ohos.base'; 1195 1196let httpResponseCache = http.createHttpResponseCache(); 1197httpResponseCache.delete().then(() => { 1198 console.info('delete success'); 1199}).catch((err: Error) => { 1200 console.info('delete fail'); 1201}); 1202``` 1203 1204## HttpDataType<sup>9+</sup> 1205 1206http的数据类型。 1207 1208**系统能力**:SystemCapability.Communication.NetStack 1209 1210| 名称 | 值 | 说明 | 1211| ------------------ | -- | ----------- | 1212| STRING | 0 | 字符串类型。 | 1213| OBJECT | 1 | 对象类型。 | 1214| ARRAY_BUFFER | 2 | 二进制数组类型。| 1215 1216## HttpProtocol<sup>9+</sup> 1217 1218http协议版本。 1219 1220**系统能力**:SystemCapability.Communication.NetStack 1221 1222| 名称 | 说明 | 1223| :-------- | :----------- | 1224| HTTP1_1 | 协议http1.1 | 1225| HTTP2 | 协议http2 | 1226