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```js 13import http from '@ohos.net.http'; 14``` 15 16## 完整示例 17 18```js 19// 引入包名 20import http from '@ohos.net.http'; 21 22// 每一个httpRequest对应一个HTTP请求任务,不可复用 23let httpRequest = http.createHttp(); 24// 用于订阅HTTP响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息 25// 从API 8开始,使用on('headersReceive', Callback)替代on('headerReceive', AsyncCallback)。 8+ 26httpRequest.on('headersReceive', (header) => { 27 console.info('header: ' + JSON.stringify(header)); 28}); 29httpRequest.request( 30 // 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定 31 "EXAMPLE_URL", 32 { 33 method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET 34 // 开发者根据自身业务需要添加header字段 35 header: { 36 'Content-Type': 'application/json' 37 }, 38 // 当使用POST请求时此字段用于传递内容 39 extraData: { 40 "data": "data to send", 41 }, 42 expectDataType: http.HttpDataType.STRING, // 可选,指定返回数据的类型 43 usingCache: true, // 可选,默认为true 44 priority: 1, // 可选,默认为1 45 connectTimeout: 60000, // 可选,默认为60000ms 46 readTimeout: 60000, // 可选,默认为60000ms 47 usingProtocol: http.HttpProtocol.HTTP1_1, // 可选,协议类型默认值由系统自动指定 48 }, (err, data) => { 49 if (!err) { 50 // data.result为HTTP响应内容,可根据业务需要进行解析 51 console.info('Result:' + JSON.stringify(data.result)); 52 console.info('code:' + JSON.stringify(data.responseCode)); 53 // data.header为HTTP响应头,可根据业务需要进行解析 54 console.info('header:' + JSON.stringify(data.header)); 55 console.info('cookies:' + JSON.stringify(data.cookies)); // 8+ 56 } else { 57 console.info('error:' + JSON.stringify(err)); 58 // 取消订阅HTTP响应头事件 59 httpRequest.off('headersReceive'); 60 // 当该请求使用完毕时,调用destroy方法主动销毁。 61 httpRequest.destroy(); 62 } 63 } 64); 65``` 66 67## http.createHttp 68 69createHttp(): HttpRequest 70 71创建一个HTTP请求,里面包括发起请求、中断请求、订阅/取消订阅HTTP Response Header事件。每一个HttpRequest对象对应一个HTTP请求。如需发起多个HTTP请求,须为每个HTTP请求创建对应HttpRequest对象。 72 73**系统能力**:SystemCapability.Communication.NetStack 74 75**返回值:** 76 77| 类型 | 说明 | 78| :---------- | :----------------------------------------------------------- | 79| HttpRequest | 返回一个HttpRequest对象,里面包括request、destroy、on和off方法。 | 80 81**示例:** 82 83```js 84import http from '@ohos.net.http'; 85let httpRequest = http.createHttp(); 86``` 87 88## HttpRequest 89 90HTTP请求任务。在调用HttpRequest的方法前,需要先通过[createHttp()](#httpcreatehttp)创建一个任务。 91 92### request 93 94request(url: string, callback: AsyncCallback\<HttpResponse\>):void 95 96根据URL地址,发起HTTP网络请求,使用callback方式作为异步方法。 97 98>**说明:** 99>此接口仅支持数据大小为5M以内的数据传输。 100 101**需要权限**:ohos.permission.INTERNET 102 103**系统能力**:SystemCapability.Communication.NetStack 104 105**参数:** 106 107| 参数名 | 类型 | 必填 | 说明 | 108| -------- | ---------------------------------------------- | ---- | ----------------------- | 109| url | string | 是 | 发起网络请求的URL地址。 | 110| callback | AsyncCallback\<[HttpResponse](#httpresponse)\> | 是 | 回调函数。 | 111 112**错误码:** 113 114| 错误码ID | 错误信息 | 115|---------|-------------------------------------------------------| 116| 401 | Parameter error. | 117| 201 | Permission denied. | 118| 2300003 | URL using bad/illegal format or missing URL. | 119| 2300007 | Couldn't connect to server. | 120| 2300028 | Timeout was reached. | 121| 2300052 | Server returned nothing (no headers, no data). | 122| 2300999 | Unknown Other Error. | 123 124>**错误码说明:** 125> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。 126> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html) 127 128**示例:** 129 130```js 131httpRequest.request("EXAMPLE_URL", (err, data) => { 132 if (!err) { 133 console.info('Result:' + data.result); 134 console.info('code:' + data.responseCode); 135 console.info('header:' + JSON.stringify(data.header)); 136 console.info('cookies:' + data.cookies); // 8+ 137 } else { 138 console.info('error:' + JSON.stringify(err)); 139 } 140}); 141``` 142 143### request 144 145request(url: string, options: HttpRequestOptions, callback: AsyncCallback\<HttpResponse\>):void 146 147根据URL地址和相关配置项,发起HTTP网络请求,使用callback方式作为异步方法。 148 149>**说明:** 150>此接口仅支持数据大小为5M以内的数据传输。 151 152**需要权限**:ohos.permission.INTERNET 153 154**系统能力**:SystemCapability.Communication.NetStack 155 156**参数:** 157 158| 参数名 | 类型 | 必填 | 说明 | 159| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- | 160| url | string | 是 | 发起网络请求的URL地址。 | 161| options | HttpRequestOptions | 是 | 参考[HttpRequestOptions](#httprequestoptions)。 | 162| callback | AsyncCallback\<[HttpResponse](#httpresponse)\> | 是 | 回调函数。 | 163 164**错误码:** 165 166| 错误码ID | 错误信息 | 167|---------|-------------------------------------------------------| 168| 401 | Parameter error. | 169| 201 | Permission denied. | 170| 2300001 | Unsupported protocol. | 171| 2300003 | URL using bad/illegal format or missing URL. | 172| 2300005 | Couldn't resolve proxy name. | 173| 2300006 | Couldn't resolve host name. | 174| 2300007 | Couldn't connect to server. | 175| 2300008 | Weird server reply. | 176| 2300009 | Access denied to remote resource. | 177| 2300016 | Error in the HTTP2 framing layer. | 178| 2300018 | Transferred a partial file. | 179| 2300023 | Failed writing received data to disk/application. | 180| 2300025 | Upload failed. | 181| 2300026 | Failed to open/read local data from file/application. | 182| 2300027 | Out of memory. | 183| 2300028 | Timeout was reached. | 184| 2300047 | Number of redirects hit maximum amount. | 185| 2300052 | Server returned nothing (no headers, no data). | 186| 2300055 | Failed sending data to the peer. | 187| 2300056 | Failure when receiving data from the peer. | 188| 2300058 | Problem with the local SSL certificate. | 189| 2300059 | Couldn't use specified SSL cipher. | 190| 2300060 | SSL peer certificate or SSH remote key was not OK. | 191| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.| 192| 2300063 | Maximum file size exceeded. | 193| 2300070 | Disk full or allocation exceeded. | 194| 2300073 | Remote file already exists. | 195| 2300077 | Problem with the SSL CA cert (path? access rights?). | 196| 2300078 | Remote file not found. | 197| 2300094 | An authentication function returned an error. | 198| 2300999 | Unknown Other Error. | 199 200>**错误码说明:** 201> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。 202> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html) 203 204**示例:** 205 206```js 207httpRequest.request("EXAMPLE_URL", 208{ 209 method: http.RequestMethod.GET, 210 header: { 211 'Content-Type': 'application/json' 212 }, 213 readTimeout: 60000, 214 connectTimeout: 60000 215}, (err, data) => { 216 if (!err) { 217 console.info('Result:' + data.result); 218 console.info('code:' + data.responseCode); 219 console.info('header:' + JSON.stringify(data.header)); 220 console.info('cookies:' + data.cookies); // 8+ 221 console.info('header.Content-Type:' + data.header['Content-Type']); 222 console.info('header.Status-Line:' + data.header['Status-Line']); 223 } else { 224 console.info('error:' + JSON.stringify(err)); 225 } 226}); 227``` 228 229### request 230 231request(url: string, options? : HttpRequestOptions): Promise\<HttpResponse\> 232 233根据URL地址,发起HTTP网络请求,使用Promise方式作为异步方法。 234 235>**说明:** 236>此接口仅支持数据大小为5M以内的数据传输。 237 238**需要权限**:ohos.permission.INTERNET 239 240**系统能力**:SystemCapability.Communication.NetStack 241 242**参数:** 243 244| 参数名 | 类型 | 必填 | 说明 | 245| ------- | ------------------ | ---- | ----------------------------------------------- | 246| url | string | 是 | 发起网络请求的URL地址。 | 247| options | HttpRequestOptions | 否 | 参考[HttpRequestOptions](#httprequestoptions)。 | 248 249**返回值:** 250 251| 类型 | 说明 | 252| :------------------------------------- | :-------------------------------- | 253| Promise<[HttpResponse](#httpresponse)> | 以Promise形式返回发起请求的结果。 | 254 255**错误码:** 256 257| 错误码ID | 错误信息 | 258|---------|-------------------------------------------------------| 259| 401 | Parameter error. | 260| 201 | Permission denied. | 261| 2300001 | Unsupported protocol. | 262| 2300003 | URL using bad/illegal format or missing URL. | 263| 2300005 | Couldn't resolve proxy name. | 264| 2300006 | Couldn't resolve host name. | 265| 2300007 | Couldn't connect to server. | 266| 2300008 | Weird server reply. | 267| 2300009 | Access denied to remote resource. | 268| 2300016 | Error in the HTTP2 framing layer. | 269| 2300018 | Transferred a partial file. | 270| 2300023 | Failed writing received data to disk/application. | 271| 2300025 | Upload failed. | 272| 2300026 | Failed to open/read local data from file/application. | 273| 2300027 | Out of memory. | 274| 2300028 | Timeout was reached. | 275| 2300047 | Number of redirects hit maximum amount. | 276| 2300052 | Server returned nothing (no headers, no data). | 277| 2300055 | Failed sending data to the peer. | 278| 2300056 | Failure when receiving data from the peer. | 279| 2300058 | Problem with the local SSL certificate. | 280| 2300059 | Couldn't use specified SSL cipher. | 281| 2300060 | SSL peer certificate or SSH remote key was not OK. | 282| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.| 283| 2300063 | Maximum file size exceeded. | 284| 2300070 | Disk full or allocation exceeded. | 285| 2300073 | Remote file already exists. | 286| 2300077 | Problem with the SSL CA cert (path? access rights?). | 287| 2300078 | Remote file not found. | 288| 2300094 | An authentication function returned an error. | 289| 2300999 | Unknown Other Error. | 290 291>**错误码说明:** 292> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。 293> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html) 294 295**示例:** 296 297```js 298let promise = httpRequest.request("EXAMPLE_URL", { 299 method: http.RequestMethod.GET, 300 connectTimeout: 60000, 301 readTimeout: 60000, 302 header: { 303 'Content-Type': 'application/json' 304 } 305}); 306promise.then((data) => { 307 console.info('Result:' + data.result); 308 console.info('code:' + data.responseCode); 309 console.info('header:' + JSON.stringify(data.header)); 310 console.info('cookies:' + data.cookies); // 8+ 311 console.info('header.Content-Type:' + data.header['Content-Type']); 312 console.info('header.Status-Line:' + data.header['Status-Line']); 313}).catch((err) => { 314 console.info('error:' + JSON.stringify(err)); 315}); 316``` 317 318### destroy 319 320destroy(): void 321 322中断请求任务。 323 324**系统能力**:SystemCapability.Communication.NetStack 325 326**示例:** 327 328```js 329httpRequest.destroy(); 330``` 331 332``` 333 334### on('headerReceive') 335 336on(type: 'headerReceive', callback: AsyncCallback\<Object\>): void 337 338订阅HTTP Response Header 事件。 339 340>**说明:** 341>此接口已废弃,建议使用[on('headersReceive')<sup>8+</sup>](#onheadersreceive8)替代。 342 343**系统能力**:SystemCapability.Communication.NetStack 344 345**参数:** 346 347| 参数名 | 类型 | 必填 | 说明 | 348| -------- | ----------------------- | ---- | --------------------------------- | 349| type | string | 是 | 订阅的事件类型,'headerReceive'。 | 350| callback | AsyncCallback\<Object\> | 是 | 回调函数。 | 351 352**示例:** 353 354```js 355httpRequest.on('headerReceive', (data) => { 356 console.info('error:' + JSON.stringify(data)); 357}); 358``` 359 360### off('headerReceive') 361 362off(type: 'headerReceive', callback?: AsyncCallback\<Object\>): void 363 364取消订阅HTTP Response Header 事件。 365 366>**说明:** 367> 368>1. 此接口已废弃,建议使用[off('headersReceive')<sup>8+</sup>](#offheadersreceive8)替代。 369> 370>2. 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。 371 372**系统能力**:SystemCapability.Communication.NetStack 373 374**参数:** 375 376| 参数名 | 类型 | 必填 | 说明 | 377| -------- | ----------------------- | ---- | ------------------------------------- | 378| type | string | 是 | 取消订阅的事件类型,'headerReceive'。 | 379| callback | AsyncCallback\<Object\> | 否 | 回调函数。 | 380 381**示例:** 382 383```js 384httpRequest.off('headerReceive'); 385``` 386 387### on('headersReceive')<sup>8+</sup> 388 389on(type: 'headersReceive', callback: Callback\<Object\>): void 390 391订阅HTTP Response Header 事件。 392 393**系统能力**:SystemCapability.Communication.NetStack 394 395**参数:** 396 397| 参数名 | 类型 | 必填 | 说明 | 398| -------- | ------------------ | ---- | ---------------------------------- | 399| type | string | 是 | 订阅的事件类型:'headersReceive'。 | 400| callback | Callback\<Object\> | 是 | 回调函数。 | 401 402**示例:** 403 404```js 405httpRequest.on('headersReceive', (header) => { 406 console.info('header: ' + JSON.stringify(header)); 407}); 408``` 409 410### off('headersReceive')<sup>8+</sup> 411 412off(type: 'headersReceive', callback?: Callback\<Object\>): void 413 414取消订阅HTTP Response Header 事件。 415 416>**说明:** 417>可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。 418 419**系统能力**:SystemCapability.Communication.NetStack 420 421**参数:** 422 423| 参数名 | 类型 | 必填 | 说明 | 424| -------- | ------------------ | ---- | -------------------------------------- | 425| type | string | 是 | 取消订阅的事件类型:'headersReceive'。 | 426| callback | Callback\<Object\> | 否 | 回调函数。 | 427 428**示例:** 429 430```js 431httpRequest.off('headersReceive'); 432``` 433 434### once('headersReceive')<sup>8+</sup> 435 436once(type: 'headersReceive', callback: Callback\<Object\>): void 437 438订阅HTTP Response Header 事件,但是只触发一次。一旦触发之后,订阅器就会被移除。使用callback方式作为异步方法。 439 440**系统能力**:SystemCapability.Communication.NetStack 441 442**参数:** 443 444| 参数名 | 类型 | 必填 | 说明 | 445| -------- | ------------------ | ---- | ---------------------------------- | 446| type | string | 是 | 订阅的事件类型:'headersReceive'。 | 447| callback | Callback\<Object\> | 是 | 回调函数。 | 448 449**示例:** 450 451```js 452httpRequest.once('headersReceive', (header) => { 453 console.info('header: ' + JSON.stringify(header)); 454}); 455``` 456 457## HttpRequestOptions 458 459发起请求可选参数的类型和取值范围。 460 461**系统能力**:SystemCapability.Communication.NetStack 462 463| 名称 | 类型 | 必填 | 说明 | 464| -------------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 465| method | [RequestMethod](#requestmethod) | 否 | 请求方式。 | 466| extraData | string \| Object \| ArrayBuffer<sup>6+</sup> | 否 | 发送请求的额外数据。<br />- 当HTTP请求为POST、PUT等方法时,此字段为HTTP请求的content。<br />- 当HTTP请求为GET、OPTIONS、DELETE、TRACE、CONNECT等方法时,此字段为HTTP请求的参数补充,参数内容会拼接到URL中进行发送。<sup>6+</sup><br />- 开发者传入string对象,开发者需要自行编码,将编码后的string传入。<sup>6+</sup> | 467| expectDataType<sup>9+</sup> | [HttpDataType](#httpdatatype9) | 否 | 指定返回数据的类型。如果设置了此参数,系统将优先返回指定的类型。 | 468| usingCache<sup>9+</sup> | boolean | 否 | 是否使用缓存,默认为true。 | 469| priority<sup>9+</sup> | number | 否 | 优先级,范围\[1,1000],默认是1。 | 470| header | Object | 否 | HTTP请求头字段。默认{'Content-Type': 'application/json'}。 | 471| readTimeout | number | 否 | 读取超时时间。单位为毫秒(ms),默认为60000ms。 | 472| connectTimeout | number | 否 | 连接超时时间。单位为毫秒(ms),默认为60000ms。 | 473| usingProtocol<sup>9+</sup> | [HttpProtocol](#httpprotocol9) | 否 | 使用协议。默认值由系统自动指定。 | 474 475## RequestMethod 476 477HTTP 请求方法。 478 479**系统能力**:SystemCapability.Communication.NetStack 480 481| 名称 | 值 | 说明 | 482| :------ | ------- | :------------------ | 483| OPTIONS | "OPTIONS" | HTTP 请求 OPTIONS。 | 484| GET | "GET" | HTTP 请求 GET。 | 485| HEAD | "HEAD" | HTTP 请求 HEAD。 | 486| POST | "POST" | HTTP 请求 POST。 | 487| PUT | "PUT" | HTTP 请求 PUT。 | 488| DELETE | "DELETE" | HTTP 请求 DELETE。 | 489| TRACE | "TRACE" | HTTP 请求 TRACE。 | 490| CONNECT | "CONNECT" | HTTP 请求 CONNECT。 | 491 492## ResponseCode 493 494发起请求返回的响应码。 495 496**系统能力**:SystemCapability.Communication.NetStack 497 498| 名称 | 值 | 说明 | 499| ----------------- | ---- | ------------------------------------------------------------ | 500| OK | 200 | 请求成功。一般用于GET与POST请求。 | 501| CREATED | 201 | 已创建。成功请求并创建了新的资源。 | 502| ACCEPTED | 202 | 已接受。已经接受请求,但未处理完成。 | 503| NOT_AUTHORITATIVE | 203 | 非授权信息。请求成功。 | 504| NO_CONTENT | 204 | 无内容。服务器成功处理,但未返回内容。 | 505| RESET | 205 | 重置内容。 | 506| PARTIAL | 206 | 部分内容。服务器成功处理了部分GET请求。 | 507| MULT_CHOICE | 300 | 多种选择。 | 508| MOVED_PERM | 301 | 永久移动。请求的资源已被永久的移动到新URI,返回信息会包括新的URI,浏览器会自动定向到新URI。 | 509| MOVED_TEMP | 302 | 临时移动。 | 510| SEE_OTHER | 303 | 查看其它地址。 | 511| NOT_MODIFIED | 304 | 未修改。 | 512| USE_PROXY | 305 | 使用代理。 | 513| BAD_REQUEST | 400 | 客户端请求的语法错误,服务器无法理解。 | 514| UNAUTHORIZED | 401 | 请求要求用户的身份认证。 | 515| PAYMENT_REQUIRED | 402 | 保留,将来使用。 | 516| FORBIDDEN | 403 | 服务器理解请求客户端的请求,但是拒绝执行此请求。 | 517| NOT_FOUND | 404 | 服务器无法根据客户端的请求找到资源(网页)。 | 518| BAD_METHOD | 405 | 客户端请求中的方法被禁止。 | 519| NOT_ACCEPTABLE | 406 | 服务器无法根据客户端请求的内容特性完成请求。 | 520| PROXY_AUTH | 407 | 请求要求代理的身份认证。 | 521| CLIENT_TIMEOUT | 408 | 请求时间过长,超时。 | 522| CONFLICT | 409 | 服务器完成客户端的PUT请求是可能返回此代码,服务器处理请求时发生了冲突。 | 523| GONE | 410 | 客户端请求的资源已经不存在。 | 524| LENGTH_REQUIRED | 411 | 服务器无法处理客户端发送的不带Content-Length的请求信息。 | 525| PRECON_FAILED | 412 | 客户端请求信息的先决条件错误。 | 526| ENTITY_TOO_LARGE | 413 | 由于请求的实体过大,服务器无法处理,因此拒绝请求。 | 527| REQ_TOO_LONG | 414 | 请求的URI过长(URI通常为网址),服务器无法处理。 | 528| UNSUPPORTED_TYPE | 415 | 服务器无法处理请求的格式。 | 529| INTERNAL_ERROR | 500 | 服务器内部错误,无法完成请求。 | 530| NOT_IMPLEMENTED | 501 | 服务器不支持请求的功能,无法完成请求。 | 531| BAD_GATEWAY | 502 | 充当网关或代理的服务器,从远端服务器接收到了一个无效的请求。 | 532| UNAVAILABLE | 503 | 由于超载或系统维护,服务器暂时的无法处理客户端的请求。 | 533| GATEWAY_TIMEOUT | 504 | 充当网关或代理的服务器,未及时从远端服务器获取请求。 | 534| VERSION | 505 | 服务器请求的HTTP协议的版本。 | 535 536## HttpResponse 537 538request方法回调函数的返回值类型。 539 540**系统能力**:SystemCapability.Communication.NetStack 541 542| 名称 | 类型 | 必填 | 说明 | 543| -------------------- | -------------------------------------------- | ---- | ------------------------------------------------------------ | 544| result | string \| Object \| ArrayBuffer<sup>6+</sup> | 是 | HTTP请求根据响应头中Content-type类型返回对应的响应格式内容:<br />- application/json:返回JSON格式的字符串,如需HTTP响应具体内容,需开发者自行解析<br />- application/octet-stream:ArrayBuffer<br />- 其他:string | 545| resultType<sup>9+</sup> | [HttpDataType](#httpdatatype9) | 是 | 返回值类型。 | 546| responseCode | [ResponseCode](#responsecode) \| number | 是 | 回调函数执行成功时,此字段为[ResponseCode](#responsecode)。若执行失败,错误码将会从AsyncCallback中的err字段返回。 | 547| 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']; | 548| cookies<sup>8+</sup> | string | 是 | 服务器返回的 cookies。 | 549 550## http.createHttpResponseCache<sup>9+</sup> 551 552createHttpResponseCache(cacheSize?: number): HttpResponseCache 553 554创建一个默认的对象来存储HTTP访问请求的响应。 555 556**系统能力**:SystemCapability.Communication.NetStack 557 558**参数:** 559 560| 参数名 | 类型 | 必填 | 说明 | 561| -------- | --------------------------------------- | ---- | ---------- | 562| cacheSize | number | 否 | 缓存大小最大为10\*1024\*1024(10MB),默认最大。 | 563 564**返回值:** 565 566| 类型 | 说明 | 567| :---------- | :----------------------------------------------------------- | 568| [HttpResponseCache](#httpresponsecache9) | 返回一个存储HTTP访问请求响应的对象。 | 569 570**示例:** 571 572```js 573import http from '@ohos.net.http'; 574let httpResponseCache = http.createHttpResponseCache(); 575``` 576 577## HttpResponseCache<sup>9+</sup> 578 579存储HTTP访问请求响应的对象。在调用HttpResponseCache的方法前,需要先通过[createHttpResponseCache()](#httpcreatehttpresponsecache9)创建一个任务。 580 581### flush<sup>9+</sup> 582 583flush(callback: AsyncCallback\<void\>): void 584 585将缓存中的数据写入文件系统,以便在下一个HTTP请求中访问所有缓存数据,使用callback方式作为异步方法。 586 587**系统能力**:SystemCapability.Communication.NetStack 588 589**参数:** 590 591| 参数名 | 类型 | 必填 | 说明 | 592| -------- | --------------------------------------- | ---- | ---------- | 593| callback | AsyncCallback\<void\> | 是 | 回调函数返回写入结果。 | 594 595**示例:** 596 597```js 598httpResponseCache.flush(err => { 599 if (err) { 600 console.info('flush fail'); 601 return; 602 } 603 console.info('flush success'); 604}); 605``` 606 607### flush<sup>9+</sup> 608 609flush(): Promise\<void\> 610 611将缓存中的数据写入文件系统,以便在下一个HTTP请求中访问所有缓存数据,使用Promise方式作为异步方法。 612 613**系统能力**:SystemCapability.Communication.NetStack 614 615**返回值:** 616 617| 类型 | 说明 | 618| --------------------------------- | ------------------------------------- | 619| Promise\<void\> | 以Promise形式返回写入结果。 | 620 621**示例:** 622 623```js 624httpResponseCache.flush().then(() => { 625 console.info('flush success'); 626}).catch(err => { 627 console.info('flush fail'); 628}); 629``` 630 631### delete<sup>9+</sup> 632 633delete(callback: AsyncCallback\<void\>): void 634 635禁用缓存并删除其中的数据,使用callback方式作为异步方法。 636 637**系统能力**:SystemCapability.Communication.NetStack 638 639**参数:** 640 641| 参数名 | 类型 | 必填 | 说明 | 642| -------- | --------------------------------------- | ---- | ---------- | 643| callback | AsyncCallback\<void\> | 是 | 回调函数返回删除结果。| 644 645**示例:** 646 647```js 648httpResponseCache.delete(err => { 649 if (err) { 650 console.info('delete fail'); 651 return; 652 } 653 console.info('delete success'); 654}); 655``` 656### delete<sup>9+</sup> 657 658delete(): Promise\<void\> 659 660禁用缓存并删除其中的数据,使用Promise方式作为异步方法。 661 662**系统能力**:SystemCapability.Communication.NetStack 663 664**返回值:** 665 666| 类型 | 说明 | 667| --------------------------------- | ------------------------------------- | 668| Promise\<void\> | 以Promise形式返回删除结果。 | 669 670**示例:** 671 672```js 673httpResponseCache.delete().then(() => { 674 console.info('delete success'); 675}).catch(err => { 676 console.info('delete fail'); 677}); 678``` 679 680## HttpDataType<sup>9+</sup> 681 682http的数据类型。 683 684**系统能力**:SystemCapability.Communication.NetStack 685 686| 名称 | 值 | 说明 | 687| ------------------ | -- | ----------- | 688| STRING | 0 | 字符串类型。 | 689| OBJECT | 1 | 对象类型。 | 690| ARRAY_BUFFER | 2 | 二进制数组类型。| 691 692## HttpProtocol<sup>9+</sup> 693 694http协议版本。 695 696**系统能力**:SystemCapability.Communication.NetStack 697 698| 名称 | 说明 | 699| :-------- | :----------- | 700| HTTP1_1 | 协议http1.1 | 701| HTTP2 | 协议http2 | 702