js-apis-http.md 44.9 KB
Newer Older
1
# @ohos.net.http (数据请求)
C
clevercong 已提交
2

3
本模块提供HTTP数据请求能力。应用可以通过HTTP发起一个数据请求,支持常见的GET、POST、OPTIONS、HEAD、PUT、DELETE、TRACE、CONNECT方法。
4

L
liyufan 已提交
5
>**说明:**
C
clevercong 已提交
6 7 8 9
>
>本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
>

C
clevercong 已提交
10
## 导入模块
C
clevercong 已提交
11

Z
zengyawen 已提交
12
```js
C
clevercong 已提交
13 14 15
import http from '@ohos.net.http';
```

C
clevercong 已提交
16
## 完整示例
C
clevercong 已提交
17

Z
zengyawen 已提交
18
```js
Y
Yangys 已提交
19
// 引入包名
C
clevercong 已提交
20 21
import http from '@ohos.net.http';

22
// 每一个httpRequest对应一个HTTP请求任务,不可复用
C
clevercong 已提交
23
let httpRequest = http.createHttp();
24
// 用于订阅HTTP响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息
C
clevercong 已提交
25
// 从API 8开始,使用on('headersReceive', Callback)替代on('headerReceive', AsyncCallback)。 8+
C
clevercong 已提交
26 27
httpRequest.on('headersReceive', (header) => {
    console.info('header: ' + JSON.stringify(header));
C
clevercong 已提交
28 29
});
httpRequest.request(
30
    // 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定
C
clevercong 已提交
31 32
    "EXAMPLE_URL",
    {
C
clevercong 已提交
33
        method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET
C
clevercong 已提交
34 35 36 37 38 39 40 41
        // 开发者根据自身业务需要添加header字段
        header: {
            'Content-Type': 'application/json'
        },
        // 当使用POST请求时此字段用于传递内容
        extraData: {
            "data": "data to send",
        },
L
liyufan 已提交
42 43 44
        expectDataType: http.HttpDataType.STRING, // 可选,指定返回数据的类型
        usingCache: true, // 可选,默认为true
        priority: 1, // 可选,默认为1
45 46
        connectTimeout: 60000, // 可选,默认为60000ms
        readTimeout: 60000, // 可选,默认为60000ms
L
liyufan 已提交
47
        usingProtocol: http.HttpProtocol.HTTP1_1, // 可选,协议类型默认值由系统自动指定
Y
Yangys 已提交
48
        usingProxy: false, //可选,默认不使用网络代理,自API 10开始支持该属性
C
clevercong 已提交
49
    }, (err, data) => {
C
clevercong 已提交
50
        if (!err) {
51
            // data.result为HTTP响应内容,可根据业务需要进行解析
Y
Yangys 已提交
52 53
            console.info('Result:' + JSON.stringify(data.result));
            console.info('code:' + JSON.stringify(data.responseCode));
54
            // data.header为HTTP响应头,可根据业务需要进行解析
C
clevercong 已提交
55
            console.info('header:' + JSON.stringify(data.header));
Y
Yangys 已提交
56
            console.info('cookies:' + JSON.stringify(data.cookies)); // 8+
C
clevercong 已提交
57
        } else {
C
clevercong 已提交
58
            console.info('error:' + JSON.stringify(err));
Y
Yangys 已提交
59 60
            // 取消订阅HTTP响应头事件
            httpRequest.off('headersReceive');
C
clevercong 已提交
61 62 63 64 65 66 67
            // 当该请求使用完毕时,调用destroy方法主动销毁。
            httpRequest.destroy();
        }
    }
);
```

C
clevercong 已提交
68
## http.createHttp
C
clevercong 已提交
69

Y
Yangys 已提交
70
createHttp(): HttpRequest
C
clevercong 已提交
71

72
创建一个HTTP请求,里面包括发起请求、中断请求、订阅/取消订阅HTTP Response Header事件。每一个HttpRequest对象对应一个HTTP请求。如需发起多个HTTP请求,须为每个HTTP请求创建对应HttpRequest对象。
C
clevercong 已提交
73

C
clevercong 已提交
74
**系统能力**:SystemCapability.Communication.NetStack
C
clevercong 已提交
75

C
clevercong 已提交
76
**返回值:**
C
clevercong 已提交
77

C
clevercong 已提交
78 79
| 类型        | 说明                                                         |
| :---------- | :----------------------------------------------------------- |
80
| HttpRequest | 返回一个HttpRequest对象,里面包括request、request2、destroy、on和off方法。 |
C
clevercong 已提交
81

C
clevercong 已提交
82 83
**示例:**

Z
zengyawen 已提交
84
```js
C
clevercong 已提交
85 86 87
import http from '@ohos.net.http';
let httpRequest = http.createHttp();
```
C
clevercong 已提交
88

C
clevercong 已提交
89
## HttpRequest
C
clevercong 已提交
90

Y
Yangys 已提交
91
HTTP请求任务。在调用HttpRequest的方法前,需要先通过[createHttp()](#httpcreatehttp)创建一个任务。
C
clevercong 已提交
92

C
clevercong 已提交
93
### request
C
clevercong 已提交
94

Y
Yangys 已提交
95
request(url: string, callback: AsyncCallback\<HttpResponse\>):void
C
clevercong 已提交
96 97 98

根据URL地址,发起HTTP网络请求,使用callback方式作为异步方法。

C
clevercong 已提交
99 100
**需要权限**:ohos.permission.INTERNET

C
clevercong 已提交
101
**系统能力**:SystemCapability.Communication.NetStack
C
clevercong 已提交
102

C
clevercong 已提交
103
**参数:**
C
clevercong 已提交
104

105 106 107
| 参数名   | 类型                                           | 必填 | 说明                    |
| -------- | ---------------------------------------------- | ---- | ----------------------- |
| url      | string                                         | 是   | 发起网络请求的URL地址。 |
C
clevercong 已提交
108
| callback | AsyncCallback\<[HttpResponse](#httpresponse)\> | 是   | 回调函数。              |
C
clevercong 已提交
109

Y
Yangys 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
**错误码:**

| 错误码ID   | 错误信息                                                  |
|---------|-------------------------------------------------------|
| 401     | Parameter error.                                      |
| 201     | Permission denied.                                    |
| 2300003 | URL using bad/illegal format or missing URL.          |
| 2300007 | Couldn't connect to server.                           |
| 2300028 | Timeout was reached.                                  |
| 2300052 | Server returned nothing (no headers, no data).        |
| 2300999 | Unknown Other Error.                                  |

>**错误码说明:**
> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)

C
clevercong 已提交
126
**示例:**
C
clevercong 已提交
127

Z
zengyawen 已提交
128
```js
C
clevercong 已提交
129
httpRequest.request("EXAMPLE_URL", (err, data) => {
C
clevercong 已提交
130 131 132 133 134 135 136 137
    if (!err) {
        console.info('Result:' + data.result);
        console.info('code:' + data.responseCode);
        console.info('header:' + JSON.stringify(data.header));
        console.info('cookies:' + data.cookies); // 8+
    } else {
        console.info('error:' + JSON.stringify(err));
    }
C
clevercong 已提交
138 139
});
```
C
clevercong 已提交
140

C
clevercong 已提交
141
### request
C
clevercong 已提交
142

Y
Yangys 已提交
143
request(url: string, options: HttpRequestOptions, callback: AsyncCallback\<HttpResponse\>):void
C
clevercong 已提交
144 145 146

根据URL地址和相关配置项,发起HTTP网络请求,使用callback方式作为异步方法。

C
clevercong 已提交
147 148
**需要权限**:ohos.permission.INTERNET

C
clevercong 已提交
149
**系统能力**:SystemCapability.Communication.NetStack
C
clevercong 已提交
150

C
clevercong 已提交
151
**参数:**
C
clevercong 已提交
152

C
clevercong 已提交
153 154 155 156 157
| 参数名   | 类型                                           | 必填 | 说明                                            |
| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- |
| url      | string                                         | 是   | 发起网络请求的URL地址。                         |
| options  | HttpRequestOptions                             | 是   | 参考[HttpRequestOptions](#httprequestoptions)。 |
| callback | AsyncCallback\<[HttpResponse](#httpresponse)\> | 是   | 回调函数。                                      |
C
clevercong 已提交
158

Y
Yangys 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
**错误码:**

| 错误码ID   | 错误信息                                                  |
|---------|-------------------------------------------------------|
| 401     | Parameter error.                                      |
| 201     | Permission denied.                                    |
| 2300001 | Unsupported protocol.                                 |
| 2300003 | URL using bad/illegal format or missing URL.          |
| 2300005 | Couldn't resolve proxy name.                          |
| 2300006 | Couldn't resolve host name.                           |
| 2300007 | Couldn't connect to server.                           |
| 2300008 | Weird server reply.                                   |
| 2300009 | Access denied to remote resource.                     |
| 2300016 | Error in the HTTP2 framing layer.                     |
| 2300018 | Transferred a partial file.                           |
| 2300023 | Failed writing received data to disk/application.     |
| 2300025 | Upload failed.                                        |
| 2300026 | Failed to open/read local data from file/application. |
| 2300027 | Out of memory.                                        |
| 2300028 | Timeout was reached.                                  |
| 2300047 | Number of redirects hit maximum amount.               |
| 2300052 | Server returned nothing (no headers, no data).        |
| 2300055 | Failed sending data to the peer.                      |
| 2300056 | Failure when receiving data from the peer.            |
| 2300058 | Problem with the local SSL certificate.               |
| 2300059 | Couldn't use specified SSL cipher.                    |
| 2300060 | SSL peer certificate or SSH remote key was not OK.    |
| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.|
| 2300063 | Maximum file size exceeded.                           |
| 2300070 | Disk full or allocation exceeded.                     |
| 2300073 | Remote file already exists.                           |
| 2300077 | Problem with the SSL CA cert (path? access rights?).  |
| 2300078 | Remote file not found.                                |
| 2300094 | An authentication function returned an error.         |
| 2300999 | Unknown Other Error.                                  |

>**错误码说明:**
> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)

C
clevercong 已提交
199
**示例:**
C
clevercong 已提交
200

Z
zengyawen 已提交
201
```js
C
clevercong 已提交
202 203
httpRequest.request("EXAMPLE_URL",
{
C
clevercong 已提交
204
    method: http.RequestMethod.GET,
C
clevercong 已提交
205 206 207 208 209 210 211 212 213
    header: {
        'Content-Type': 'application/json'
    },
    readTimeout: 60000,
    connectTimeout: 60000
}, (err, data) => {
    if (!err) {
        console.info('Result:' + data.result);
        console.info('code:' + data.responseCode);
C
clevercong 已提交
214
        console.info('header:' + JSON.stringify(data.header));
C
clevercong 已提交
215 216 217 218
        console.info('cookies:' + data.cookies); // 8+
        console.info('header.Content-Type:' + data.header['Content-Type']);
        console.info('header.Status-Line:' + data.header['Status-Line']);
    } else {
C
clevercong 已提交
219
        console.info('error:' + JSON.stringify(err));
C
clevercong 已提交
220
    }
C
clevercong 已提交
221 222
});
```
C
clevercong 已提交
223

C
clevercong 已提交
224
### request
C
clevercong 已提交
225

Y
Yangys 已提交
226
request(url: string, options? : HttpRequestOptions): Promise\<HttpResponse\>
C
clevercong 已提交
227 228 229

根据URL地址,发起HTTP网络请求,使用Promise方式作为异步方法。

C
clevercong 已提交
230 231
**需要权限**:ohos.permission.INTERNET

C
clevercong 已提交
232
**系统能力**:SystemCapability.Communication.NetStack
C
clevercong 已提交
233

C
clevercong 已提交
234 235
**参数:**

236 237 238
| 参数名  | 类型               | 必填 | 说明                                            |
| ------- | ------------------ | ---- | ----------------------------------------------- |
| url     | string             | 是   | 发起网络请求的URL地址。                         |
L
liyufan 已提交
239
| options | HttpRequestOptions | 否   | 参考[HttpRequestOptions](#httprequestoptions)。 |
C
clevercong 已提交
240

C
clevercong 已提交
241 242
**返回值:**

243 244
| 类型                                   | 说明                              |
| :------------------------------------- | :-------------------------------- |
C
clevercong 已提交
245
| Promise<[HttpResponse](#httpresponse)> | 以Promise形式返回发起请求的结果。 |
C
clevercong 已提交
246

Y
Yangys 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
**错误码:**

| 错误码ID   | 错误信息                                                  |
|---------|-------------------------------------------------------|
| 401     | Parameter error.                                      |
| 201     | Permission denied.                                    |
| 2300001 | Unsupported protocol.                                 |
| 2300003 | URL using bad/illegal format or missing URL.          |
| 2300005 | Couldn't resolve proxy name.                          |
| 2300006 | Couldn't resolve host name.                           |
| 2300007 | Couldn't connect to server.                           |
| 2300008 | Weird server reply.                                   |
| 2300009 | Access denied to remote resource.                     |
| 2300016 | Error in the HTTP2 framing layer.                     |
| 2300018 | Transferred a partial file.                           |
| 2300023 | Failed writing received data to disk/application.     |
| 2300025 | Upload failed.                                        |
| 2300026 | Failed to open/read local data from file/application. |
| 2300027 | Out of memory.                                        |
| 2300028 | Timeout was reached.                                  |
| 2300047 | Number of redirects hit maximum amount.               |
| 2300052 | Server returned nothing (no headers, no data).        |
| 2300055 | Failed sending data to the peer.                      |
| 2300056 | Failure when receiving data from the peer.            |
| 2300058 | Problem with the local SSL certificate.               |
| 2300059 | Couldn't use specified SSL cipher.                    |
| 2300060 | SSL peer certificate or SSH remote key was not OK.    |
| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.|
| 2300063 | Maximum file size exceeded.                           |
| 2300070 | Disk full or allocation exceeded.                     |
| 2300073 | Remote file already exists.                           |
| 2300077 | Problem with the SSL CA cert (path? access rights?).  |
| 2300078 | Remote file not found.                                |
| 2300094 | An authentication function returned an error.         |
| 2300999 | Unknown Other Error.                                  |

>**错误码说明:**
> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html) 
C
clevercong 已提交
286 287 288

**示例:**

Z
zengyawen 已提交
289
```js
C
clevercong 已提交
290
let promise = httpRequest.request("EXAMPLE_URL", {
C
clevercong 已提交
291
    method: http.RequestMethod.GET,
C
clevercong 已提交
292 293 294 295 296
    connectTimeout: 60000,
    readTimeout: 60000,
    header: {
        'Content-Type': 'application/json'
    }
C
clevercong 已提交
297
});
C
clevercong 已提交
298 299 300 301 302 303 304
promise.then((data) => {
    console.info('Result:' + data.result);
    console.info('code:' + data.responseCode);
    console.info('header:' + JSON.stringify(data.header));
    console.info('cookies:' + data.cookies); // 8+
    console.info('header.Content-Type:' + data.header['Content-Type']);
    console.info('header.Status-Line:' + data.header['Status-Line']);
C
clevercong 已提交
305
}).catch((err) => {
C
clevercong 已提交
306
    console.info('error:' + JSON.stringify(err));
C
clevercong 已提交
307 308
});
```
C
clevercong 已提交
309

C
clevercong 已提交
310
### destroy
C
clevercong 已提交
311

Y
Yangys 已提交
312
destroy(): void
C
clevercong 已提交
313 314 315

中断请求任务。

C
clevercong 已提交
316
**系统能力**:SystemCapability.Communication.NetStack
C
clevercong 已提交
317

C
clevercong 已提交
318
**示例:**
C
clevercong 已提交
319

Z
zengyawen 已提交
320
```js
C
clevercong 已提交
321 322
httpRequest.destroy();
```
C
clevercong 已提交
323

Y
Yangys 已提交
324 325
### request2<sup>10+</sup>

Y
Yangys 已提交
326
request2(url: string, callback: AsyncCallback\<void\>): void
Y
Yangys 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361

根据URL地址和相关配置项,发起HTTP网络请求并返回流式响应,使用callback方式作为异步方法。

**需要权限**:ohos.permission.INTERNET

**系统能力**:SystemCapability.Communication.NetStack

**参数:**

| 参数名   | 类型                                           | 必填 | 说明                                            |
| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- |
| url      | string                                         | 是   | 发起网络请求的URL地址。                         |
| callback | AsyncCallback\<void\>                          | 是   | 回调函数。                                      |

**错误码:**

| 错误码ID   | 错误信息                                                  |
|---------|-------------------------------------------------------|
| 401     | Parameter error.                                      |
| 201     | Permission denied.                                    |
| 2300003 | URL using bad/illegal format or missing URL.          |
| 2300007 | Couldn't connect to server.                           |
| 2300028 | Timeout was reached.                                  |
| 2300052 | Server returned nothing (no headers, no data).        |
| 2300999 | Unknown Other Error.                                  |

>**错误码说明:**
> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)

**示例:**

```js
httpRequest.request2("EXAMPLE_URL", (err) => {
    if (!err) {
Y
Yangys 已提交
362
        console.info("request2 OK!");
Y
Yangys 已提交
363 364 365 366 367 368 369 370
    } else {
        console.info("request2 ERROR : err = " + JSON.stringify(err));
    }
})
```

### request2<sup>10+</sup>

Y
Yangys 已提交
371
request2(url: string, options: HttpRequestOptions, callback: AsyncCallback\<void\>): void
Y
Yangys 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

根据URL地址和相关配置项,发起HTTP网络请求并返回流式响应,使用callback方式作为异步方法。

**需要权限**:ohos.permission.INTERNET

**系统能力**:SystemCapability.Communication.NetStack

**参数:**

| 参数名   | 类型                                           | 必填 | 说明                                            |
| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- |
| url      | string                                         | 是   | 发起网络请求的URL地址。                         |
| options  | HttpRequestOptions                             | 是   | 参考[HttpRequestOptions](#httprequestoptions)。 |
| callback | AsyncCallback\<void\>                          | 是   | 回调函数。                                      |

**错误码:**

| 错误码ID   | 错误信息                                                  |
|---------|-------------------------------------------------------|
| 401     | Parameter error.                                      |
| 201     | Permission denied.                                    |
| 2300001 | Unsupported protocol.                                 |
| 2300003 | URL using bad/illegal format or missing URL.          |
| 2300005 | Couldn't resolve proxy name.                          |
| 2300006 | Couldn't resolve host name.                           |
| 2300007 | Couldn't connect to server.                           |
| 2300008 | Weird server reply.                                   |
| 2300009 | Access denied to remote resource.                     |
| 2300016 | Error in the HTTP2 framing layer.                     |
| 2300018 | Transferred a partial file.                           |
| 2300023 | Failed writing received data to disk/application.     |
| 2300025 | Upload failed.                                        |
| 2300026 | Failed to open/read local data from file/application. |
| 2300027 | Out of memory.                                        |
| 2300028 | Timeout was reached.                                  |
| 2300047 | Number of redirects hit maximum amount.               |
| 2300052 | Server returned nothing (no headers, no data).        |
| 2300055 | Failed sending data to the peer.                      |
| 2300056 | Failure when receiving data from the peer.            |
| 2300058 | Problem with the local SSL certificate.               |
| 2300059 | Couldn't use specified SSL cipher.                    |
| 2300060 | SSL peer certificate or SSH remote key was not OK.    |
| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.|
| 2300063 | Maximum file size exceeded.                           |
| 2300070 | Disk full or allocation exceeded.                     |
| 2300073 | Remote file already exists.                           |
| 2300077 | Problem with the SSL CA cert (path? access rights?).  |
| 2300078 | Remote file not found.                                |
| 2300094 | An authentication function returned an error.         |
| 2300999 | Unknown Other Error.                                  |

>**错误码说明:**
> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)

**示例:**

```js
httpRequest.request2("EXAMPLE_URL",
{
    method: http.RequestMethod.GET,
    header: {
        'Content-Type': 'application/json'
    },
    readTimeout: 60000,
    connectTimeout: 60000
}, (err) => {
    if (!err) {
Y
Yangys 已提交
440
        console.info("request2 OK!");
Y
Yangys 已提交
441 442 443 444 445 446 447
    } else {
        console.info("request2 ERROR : err = " + JSON.stringify(err));
    }
})
```
### request2<sup>10+</sup>

Y
Yangys 已提交
448
request2(url: string, options? : HttpRequestOptions): Promise\<void\>
Y
Yangys 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

根据URL地址,发起HTTP网络请求并返回流式响应,使用Promise方式作为异步方法。

**需要权限**:ohos.permission.INTERNET

**系统能力**:SystemCapability.Communication.NetStack

**参数:**

| 参数名  | 类型               | 必填 | 说明                                            |
| ------- | ------------------ | ---- | ----------------------------------------------- |
| url     | string             | 是   | 发起网络请求的URL地址。                         |
| options | HttpRequestOptions | 否   | 参考[HttpRequestOptions](#httprequestoptions)。 |

**返回值:**

| 类型                                   | 说明                              |
| :------------------------------------- | :-------------------------------- |
| Promise\<void\> | 以Promise形式返回发起请求的结果。 |

**错误码:**

| 错误码ID   | 错误信息                                                  |
|---------|-------------------------------------------------------|
| 401     | Parameter error.                                      |
| 201     | Permission denied.                                    |
| 2300001 | Unsupported protocol.                                 |
| 2300003 | URL using bad/illegal format or missing URL.          |
| 2300005 | Couldn't resolve proxy name.                          |
| 2300006 | Couldn't resolve host name.                           |
| 2300007 | Couldn't connect to server.                           |
| 2300008 | Weird server reply.                                   |
| 2300009 | Access denied to remote resource.                     |
| 2300016 | Error in the HTTP2 framing layer.                     |
| 2300018 | Transferred a partial file.                           |
| 2300023 | Failed writing received data to disk/application.     |
| 2300025 | Upload failed.                                        |
| 2300026 | Failed to open/read local data from file/application. |
| 2300027 | Out of memory.                                        |
| 2300028 | Timeout was reached.                                  |
| 2300047 | Number of redirects hit maximum amount.               |
| 2300052 | Server returned nothing (no headers, no data).        |
| 2300055 | Failed sending data to the peer.                      |
| 2300056 | Failure when receiving data from the peer.            |
| 2300058 | Problem with the local SSL certificate.               |
| 2300059 | Couldn't use specified SSL cipher.                    |
| 2300060 | SSL peer certificate or SSH remote key was not OK.    |
| 2300061 | Unrecognized or bad HTTP Content or Transfer-Encoding.|
| 2300063 | Maximum file size exceeded.                           |
| 2300070 | Disk full or allocation exceeded.                     |
| 2300073 | Remote file already exists.                           |
| 2300077 | Problem with the SSL CA cert (path? access rights?).  |
| 2300078 | Remote file not found.                                |
| 2300094 | An authentication function returned an error.         |
| 2300999 | Unknown Other Error.                                  |

>**错误码说明:**
> 以上错误码的详细介绍参见[HTTP错误码](../errorcodes/errorcode-net-http.md)。
> HTTP 错误码映射关系:2300000 + curl错误码。更多常用错误码,可参考:

**示例:**

```js
let promise = httpRequest.request("EXAMPLE_URL", {
    method: http.RequestMethod.GET,
    connectTimeout: 60000,
    readTimeout: 60000,
    header: {
        'Content-Type': 'application/json'
    }
});
promise.then(() => {
Y
Yangys 已提交
521
    console.info("request2 OK!");
Y
Yangys 已提交
522 523 524 525 526
}).catch((err) => {
    console.info("request2 ERROR : err = " + JSON.stringify(err));
});
```

Y
Yangys 已提交
527
### on('headerReceive')
C
clevercong 已提交
528

Y
Yangys 已提交
529
on(type: 'headerReceive', callback: AsyncCallback\<Object\>): void
C
clevercong 已提交
530 531 532

订阅HTTP Response Header 事件。

L
liyufan 已提交
533
>![](public_sys-resources/icon-note.gif) **说明:**
Y
Yangys 已提交
534
>此接口已废弃,建议使用[on('headersReceive')<sup>8+</sup>](#onheadersreceive8)替代。
C
clevercong 已提交
535

C
clevercong 已提交
536
**系统能力**:SystemCapability.Communication.NetStack
C
clevercong 已提交
537

C
clevercong 已提交
538
**参数:**
C
clevercong 已提交
539

C
clevercong 已提交
540 541 542 543
| 参数名   | 类型                    | 必填 | 说明                              |
| -------- | ----------------------- | ---- | --------------------------------- |
| type     | string                  | 是   | 订阅的事件类型,'headerReceive'。 |
| callback | AsyncCallback\<Object\> | 是   | 回调函数。                        |
C
clevercong 已提交
544

C
clevercong 已提交
545
**示例:**
C
clevercong 已提交
546

Z
zengyawen 已提交
547
```js
548 549
httpRequest.on('headerReceive', (data) => {
    console.info('error:' + JSON.stringify(data));
C
clevercong 已提交
550 551
});
```
C
clevercong 已提交
552

Y
Yangys 已提交
553
### off('headerReceive')
C
clevercong 已提交
554

Y
Yangys 已提交
555
off(type: 'headerReceive', callback?: AsyncCallback\<Object\>): void
C
clevercong 已提交
556 557 558

取消订阅HTTP Response Header 事件。

L
liyufan 已提交
559
>![](public_sys-resources/icon-note.gif) **说明:**
C
clevercong 已提交
560
>
Y
Yangys 已提交
561
>1. 此接口已废弃,建议使用[off('headersReceive')<sup>8+</sup>](#offheadersreceive8)替代。
C
clevercong 已提交
562 563 564
>
>2. 可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。

C
clevercong 已提交
565
**系统能力**:SystemCapability.Communication.NetStack
C
clevercong 已提交
566

C
clevercong 已提交
567
**参数:**
C
clevercong 已提交
568

C
clevercong 已提交
569 570 571 572
| 参数名   | 类型                    | 必填 | 说明                                  |
| -------- | ----------------------- | ---- | ------------------------------------- |
| type     | string                  | 是   | 取消订阅的事件类型,'headerReceive'。 |
| callback | AsyncCallback\<Object\> | 否   | 回调函数。                            |
C
clevercong 已提交
573

C
clevercong 已提交
574
**示例:**
C
clevercong 已提交
575

Z
zengyawen 已提交
576
```js
C
clevercong 已提交
577 578
httpRequest.off('headerReceive');
```
C
clevercong 已提交
579

Y
Yangys 已提交
580
### on('headersReceive')<sup>8+</sup>
C
clevercong 已提交
581

Y
Yangys 已提交
582
on(type: 'headersReceive', callback: Callback\<Object\>): void
C
clevercong 已提交
583 584 585

订阅HTTP Response Header 事件。

C
clevercong 已提交
586
**系统能力**:SystemCapability.Communication.NetStack
C
clevercong 已提交
587

C
clevercong 已提交
588
**参数:**
C
clevercong 已提交
589

C
clevercong 已提交
590 591 592 593
| 参数名   | 类型               | 必填 | 说明                               |
| -------- | ------------------ | ---- | ---------------------------------- |
| type     | string             | 是   | 订阅的事件类型:'headersReceive'。 |
| callback | Callback\<Object\> | 是   | 回调函数。                         |
C
clevercong 已提交
594

C
clevercong 已提交
595
**示例:**
C
clevercong 已提交
596

Z
zengyawen 已提交
597
```js
C
clevercong 已提交
598 599
httpRequest.on('headersReceive', (header) => {
    console.info('header: ' + JSON.stringify(header));
C
clevercong 已提交
600 601
});
```
C
clevercong 已提交
602

Y
Yangys 已提交
603
### off('headersReceive')<sup>8+</sup>
C
clevercong 已提交
604

Y
Yangys 已提交
605
off(type: 'headersReceive', callback?: Callback\<Object\>): void
C
clevercong 已提交
606 607 608

取消订阅HTTP Response Header 事件。

L
liyufan 已提交
609
>![](public_sys-resources/icon-note.gif) **说明:**
C
clevercong 已提交
610 611
>可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。

C
clevercong 已提交
612
**系统能力**:SystemCapability.Communication.NetStack
C
clevercong 已提交
613

C
clevercong 已提交
614
**参数:**
C
clevercong 已提交
615

C
clevercong 已提交
616 617 618 619
| 参数名   | 类型               | 必填 | 说明                                   |
| -------- | ------------------ | ---- | -------------------------------------- |
| type     | string             | 是   | 取消订阅的事件类型:'headersReceive'。 |
| callback | Callback\<Object\> | 否   | 回调函数。                             |
C
clevercong 已提交
620

C
clevercong 已提交
621
**示例:**
C
clevercong 已提交
622

Z
zengyawen 已提交
623
```js
C
clevercong 已提交
624 625
httpRequest.off('headersReceive');
```
C
clevercong 已提交
626

Y
Yangys 已提交
627
### once('headersReceive')<sup>8+</sup>
C
clevercong 已提交
628

Y
Yangys 已提交
629
once(type: 'headersReceive', callback: Callback\<Object\>): void
C
clevercong 已提交
630 631 632

订阅HTTP Response Header 事件,但是只触发一次。一旦触发之后,订阅器就会被移除。使用callback方式作为异步方法。

C
clevercong 已提交
633
**系统能力**:SystemCapability.Communication.NetStack
C
clevercong 已提交
634

C
clevercong 已提交
635
**参数:**
C
clevercong 已提交
636

C
clevercong 已提交
637 638 639 640
| 参数名   | 类型               | 必填 | 说明                               |
| -------- | ------------------ | ---- | ---------------------------------- |
| type     | string             | 是   | 订阅的事件类型:'headersReceive'。 |
| callback | Callback\<Object\> | 是   | 回调函数。                         |
C
clevercong 已提交
641

C
clevercong 已提交
642
**示例:**
C
clevercong 已提交
643

Z
zengyawen 已提交
644
```js
C
clevercong 已提交
645 646
httpRequest.once('headersReceive', (header) => {
    console.info('header: ' + JSON.stringify(header));
C
clevercong 已提交
647 648
});
```
Y
Yangys 已提交
649
### on('dataReceive')<sup>10+</sup>
Y
Yangys 已提交
650

Y
Yangys 已提交
651
on(type: 'dataReceive', callback: Callback\<ArrayBuffer\>): void
Y
Yangys 已提交
652 653

订阅HTTP流式响应数据接收事件。
C
clevercong 已提交
654

Y
Yangys 已提交
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
**系统能力**:SystemCapability.Communication.NetStack

**参数:**

| 参数名   | 类型                    | 必填 | 说明                              |
| -------- | ----------------------- | ---- | --------------------------------- |
| type     | string                  | 是   | 订阅的事件类型,'dataReceive'。 |
| callback | AsyncCallback\<ArrayBuffer\> | 是   | 回调函数。                        |

**示例:**

```js
httpRequest.on('dataReceive', (data) => {
    console.info('dataReceive length: ' + JSON.stringify(data.byteLength));
});
```

Y
Yangys 已提交
672
### off('dataReceive')<sup>10+</sup>
Y
Yangys 已提交
673

Y
Yangys 已提交
674
off(type: 'dataReceive', callback?: Callback\<ArrayBuffer\>): void
Y
Yangys 已提交
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695

取消订阅HTTP流式响应数据接收事件。

>![](public_sys-resources/icon-note.gif) **说明:**
>可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。

**系统能力**:SystemCapability.Communication.NetStack

**参数:**

| 参数名   | 类型               | 必填 | 说明                                   |
| -------- | ------------------ | ---- | -------------------------------------- |
| type     | string             | 是   | 取消订阅的事件类型:'dataReceive'。 |
| callback | Callback\<ArrayBuffer\> | 否   | 回调函数。                             |

**示例:**

```js
httpRequest.off('dataReceive');
```

Y
Yangys 已提交
696
### on('dataEnd')<sup>10+</sup>
Y
Yangys 已提交
697

Y
Yangys 已提交
698
on(type: 'dataEnd', callback: Callback\<void\>): void
Y
Yangys 已提交
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718

订阅HTTP流式响应数据接收完毕事件。

**系统能力**:SystemCapability.Communication.NetStack

**参数:**

| 参数名   | 类型                    | 必填 | 说明                              |
| -------- | ----------------------- | ---- | --------------------------------- |
| type     | string                  | 是   | 订阅的事件类型,'dataEnd'。 |
| callback | AsyncCallback\<void\>   | 是   | 回调函数。                        |

**示例:**

```js
httpRequest.on('dataReceive', () => {
    console.info('Receive dataEnd!');
});
```

Y
Yangys 已提交
719
### off('dataEnd')<sup>10+</sup>
Y
Yangys 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742

off(type: 'dataEnd', callback?: Callback\<void\>): void

取消订阅HTTP流式响应数据接收完毕事件。

>![](public_sys-resources/icon-note.gif) **说明:**
>可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。

**系统能力**:SystemCapability.Communication.NetStack

**参数:**

| 参数名   | 类型               | 必填 | 说明                                   |
| -------- | ------------------ | ---- | -------------------------------------- |
| type     | string             | 是   | 取消订阅的事件类型:'dataEnd'。 |
| callback | Callback\<void\>   | 否   | 回调函数。                             |

**示例:**

```js
httpRequest.off('dataEnd');
```

Y
Yangys 已提交
743
### on('dataProgress')<sup>10+</sup>
Y
Yangys 已提交
744

Y
Yangys 已提交
745
 on(type: 'dataProgress', callback: Callback\<{ receiveSize: number, totalSize: number }\>): void
Y
Yangys 已提交
746 747 748 749 750 751 752 753 754 755

订阅HTTP流式响应数据接收进度事件。

**系统能力**:SystemCapability.Communication.NetStack

**参数:**

| 参数名   | 类型                    | 必填 | 说明                              |
| -------- | ----------------------- | ---- | --------------------------------- |
| type     | string                  | 是   | 订阅的事件类型,'dataProgress'。 |
Y
Yangys 已提交
756
| callback | AsyncCallback\<{ receiveSize: number, totalSize: number }\>   | 是   | 回调函数。<br>receiveSize:已接收的数据字节数,totalSize待接收的字节总数 |
Y
Yangys 已提交
757 758 759 760 761

**示例:**

```js
httpRequest.on('dataProgress', (data) => {
762
    console.info('dataProgress:' + JSON.stringify(data));
Y
Yangys 已提交
763 764 765
});
```

Y
Yangys 已提交
766
### off('dataProgress')<sup>10+</sup>
Y
Yangys 已提交
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788

off(type: 'dataProgress', callback?: Callback\<{ receiveSize: number, totalSize: number }\>): void

取消订阅HTTP流式响应数据接收进度事件。

>![](public_sys-resources/icon-note.gif) **说明:**
>可以指定传入on中的callback取消一个订阅,也可以不指定callback清空所有订阅。

**系统能力**:SystemCapability.Communication.NetStack

**参数:**

| 参数名   | 类型               | 必填 | 说明                                   |
| -------- | ------------------ | ---- | -------------------------------------- |
| type     | string             | 是   | 取消订阅的事件类型:'dataProgress'。 |
| callback | Callback\<{ receiveSize: number, totalSize: number }\>   | 否   | 回调函数。                             |

**示例:**

```js
httpRequest.off('dataProgress');
```
C
clevercong 已提交
789
## HttpRequestOptions
C
clevercong 已提交
790 791 792

发起请求可选参数的类型和取值范围。

C
clevercong 已提交
793 794
**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.NetStack。

L
liyufan 已提交
795
| 名称         | 类型                                          | 必填 | 说明                                                         |
796 797
| -------------- | --------------------------------------------- | ---- | ------------------------------------------------------------ |
| method         | [RequestMethod](#requestmethod)               | 否   | 请求方式。                                                   |
Z
zhuwenchao 已提交
798
| 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> |
Y
Yangys 已提交
799
| expectDataType<sup>9+</sup>  | [HttpDataType](#httpdatatype9)  | 否   | 指定返回数据的类型。如果设置了此参数,系统将优先返回指定的类型。 |
L
liyufan123 已提交
800
| usingCache<sup>9+</sup>      | boolean                         | 否   | 是否使用缓存,默认为true。   |
L
liyufan 已提交
801
| priority<sup>9+</sup>        | number                          | 否   | 优先级,范围\[1,1000],默认是1。                           |
Y
Yangys 已提交
802 803 804 805 806
| header                       | Object                          | 否   | HTTP请求头字段。默认{'Content-Type': 'application/json'}。   |
| readTimeout                  | number                          | 否   | 读取超时时间。单位为毫秒(ms),默认为60000ms。              |
| connectTimeout               | number                          | 否   | 连接超时时间。单位为毫秒(ms),默认为60000ms。              |
| usingProtocol<sup>9+</sup>   | [HttpProtocol](#httpprotocol9)  | 否   | 使用协议。默认值由系统自动指定。                             |
| usingProxy<sup>10+</sup>     | boolean \| Object               | 否   | 是否使用HTTP代理,默认为false,不使用代理。<br />- 当usingProxy为布尔类型true时,使用默认网络代理。<br />- 当usingProxy为object类型时,使用指定网络代理。                                |
C
clevercong 已提交
807

C
clevercong 已提交
808
## RequestMethod
C
clevercong 已提交
809 810 811

HTTP 请求方法。

C
clevercong 已提交
812 813 814 815
**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.NetStack。

| 名称    | 值      | 说明                |
| :------ | ------- | :------------------ |
L
liyufan 已提交
816 817 818 819 820 821 822 823
| OPTIONS | "OPTIONS" | HTTP 请求 OPTIONS。 |
| GET     | "GET"     | HTTP 请求 GET。     |
| HEAD    | "HEAD"    | HTTP 请求 HEAD。    |
| POST    | "POST"    | HTTP 请求 POST。    |
| PUT     | "PUT"     | HTTP 请求 PUT。     |
| DELETE  | "DELETE"  | HTTP 请求 DELETE。  |
| TRACE   | "TRACE"   | HTTP 请求 TRACE。   |
| CONNECT | "CONNECT" | HTTP 请求 CONNECT。 |
C
clevercong 已提交
824

C
clevercong 已提交
825
## ResponseCode
C
clevercong 已提交
826 827 828

发起请求返回的响应码。

C
clevercong 已提交
829 830 831
**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.NetStack。

| 名称              | 值   | 说明                                                         |
C
clevercong 已提交
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
| ----------------- | ---- | ------------------------------------------------------------ |
| OK                | 200  | 请求成功。一般用于GET与POST请求。                            |
| CREATED           | 201  | 已创建。成功请求并创建了新的资源。                           |
| ACCEPTED          | 202  | 已接受。已经接受请求,但未处理完成。                         |
| NOT_AUTHORITATIVE | 203  | 非授权信息。请求成功。                                       |
| NO_CONTENT        | 204  | 无内容。服务器成功处理,但未返回内容。                       |
| RESET             | 205  | 重置内容。                                                   |
| PARTIAL           | 206  | 部分内容。服务器成功处理了部分GET请求。                      |
| MULT_CHOICE       | 300  | 多种选择。                                                   |
| MOVED_PERM        | 301  | 永久移动。请求的资源已被永久的移动到新URI,返回信息会包括新的URI,浏览器会自动定向到新URI。 |
| MOVED_TEMP        | 302  | 临时移动。                                                   |
| SEE_OTHER         | 303  | 查看其它地址。                                               |
| NOT_MODIFIED      | 304  | 未修改。                                                     |
| USE_PROXY         | 305  | 使用代理。                                                   |
| BAD_REQUEST       | 400  | 客户端请求的语法错误,服务器无法理解。                       |
| UNAUTHORIZED      | 401  | 请求要求用户的身份认证。                                     |
| PAYMENT_REQUIRED  | 402  | 保留,将来使用。                                             |
| FORBIDDEN         | 403  | 服务器理解请求客户端的请求,但是拒绝执行此请求。             |
| NOT_FOUND         | 404  | 服务器无法根据客户端的请求找到资源(网页)。                 |
| BAD_METHOD        | 405  | 客户端请求中的方法被禁止。                                   |
| NOT_ACCEPTABLE    | 406  | 服务器无法根据客户端请求的内容特性完成请求。                 |
| PROXY_AUTH        | 407  | 请求要求代理的身份认证。                                     |
| CLIENT_TIMEOUT    | 408  | 请求时间过长,超时。                                         |
| CONFLICT          | 409  | 服务器完成客户端的PUT请求是可能返回此代码,服务器处理请求时发生了冲突。 |
| GONE              | 410  | 客户端请求的资源已经不存在。                                 |
| LENGTH_REQUIRED   | 411  | 服务器无法处理客户端发送的不带Content-Length的请求信息。     |
| PRECON_FAILED     | 412  | 客户端请求信息的先决条件错误。                               |
| ENTITY_TOO_LARGE  | 413  | 由于请求的实体过大,服务器无法处理,因此拒绝请求。           |
| REQ_TOO_LONG      | 414  | 请求的URI过长(URI通常为网址),服务器无法处理。             |
| UNSUPPORTED_TYPE  | 415  | 服务器无法处理请求的格式。                                   |
| INTERNAL_ERROR    | 500  | 服务器内部错误,无法完成请求。                               |
| NOT_IMPLEMENTED   | 501  | 服务器不支持请求的功能,无法完成请求。                       |
| BAD_GATEWAY       | 502  | 充当网关或代理的服务器,从远端服务器接收到了一个无效的请求。 |
| UNAVAILABLE       | 503  | 由于超载或系统维护,服务器暂时的无法处理客户端的请求。       |
| GATEWAY_TIMEOUT   | 504  | 充当网关或代理的服务器,未及时从远端服务器获取请求。         |
| VERSION           | 505  | 服务器请求的HTTP协议的版本。                                 |

C
clevercong 已提交
869
## HttpResponse
C
clevercong 已提交
870 871 872

request方法回调函数的返回值类型。

C
clevercong 已提交
873 874
**系统能力**:以下各项对应的系统能力均为SystemCapability.Communication.NetStack。

L
liyufan 已提交
875
| 名称               | 类型                                         | 必填 | 说明                                                         |
C
clevercong 已提交
876
| -------------------- | -------------------------------------------- | ---- | ------------------------------------------------------------ |
Z
zhuwenchao 已提交
877
| result               | string \| Object \| ArrayBuffer<sup>6+</sup> | 是   | HTTP请求根据响应头中Content-type类型返回对应的响应格式内容:<br />- application/json:返回JSON格式的字符串,如需HTTP响应具体内容,需开发者自行解析<br />- application/octet-stream:ArrayBuffer<br />- 其他:string |
L
liyufan 已提交
878
| resultType<sup>9+</sup> | [HttpDataType](#httpdatatype9)             | 是   | 返回值类型。                           |
Y
Yangys 已提交
879
| responseCode         | [ResponseCode](#responsecode) \| number      | 是   | 回调函数执行成功时,此字段为[ResponseCode](#responsecode)。若执行失败,错误码将会从AsyncCallback中的err字段返回。 |
880
| 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']; |
Y
Yangys 已提交
881
| cookies<sup>8+</sup> | string                                       | 是   | 服务器返回的 cookies。                                       |
C
clevercong 已提交
882

L
liyufan 已提交
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
## http.createHttpResponseCache<sup>9+</sup>

createHttpResponseCache(cacheSize?: number): HttpResponseCache

创建一个默认的对象来存储HTTP访问请求的响应。

**系统能力**:SystemCapability.Communication.NetStack

**参数:**

| 参数名   | 类型                                    | 必填 | 说明       |
| -------- | --------------------------------------- | ---- | ---------- |
| cacheSize | number | 否 | 缓存大小最大为10\*1024\*1024(10MB),默认最大。 |

**返回值:**

| 类型        | 说明                                                         |
| :---------- | :----------------------------------------------------------- |
| [HttpResponseCache](#httpresponsecache9) | 返回一个存储HTTP访问请求响应的对象。 |

**示例:**

```js
import http from '@ohos.net.http';
let httpResponseCache = http.createHttpResponseCache();
```

## HttpResponseCache<sup>9+</sup>

912
存储HTTP访问请求响应的对象。在调用HttpResponseCache的方法前,需要先通过[createHttpResponseCache()](#httpcreatehttpresponsecache9)创建一个任务。
L
liyufan 已提交
913 914 915

### flush<sup>9+</sup>

Y
Yangys 已提交
916
flush(callback: AsyncCallback\<void\>): void
L
liyufan 已提交
917 918 919 920 921 922 923 924 925

将缓存中的数据写入文件系统,以便在下一个HTTP请求中访问所有缓存数据,使用callback方式作为异步方法。

**系统能力**:SystemCapability.Communication.NetStack

**参数:**

| 参数名   | 类型                                    | 必填 | 说明       |
| -------- | --------------------------------------- | ---- | ---------- |
Y
Yangys 已提交
926
| callback | AsyncCallback\<void\> | 是   | 回调函数返回写入结果。 |
L
liyufan 已提交
927 928 929 930 931 932

**示例:**

```js
httpResponseCache.flush(err => {
  if (err) {
Y
Yangys 已提交
933
    console.info('flush fail');
L
liyufan 已提交
934 935
    return;
  }
Y
Yangys 已提交
936
  console.info('flush success');
L
liyufan 已提交
937 938 939 940 941
});
```

### flush<sup>9+</sup>

Y
Yangys 已提交
942
flush(): Promise\<void\>
L
liyufan 已提交
943 944 945 946 947

将缓存中的数据写入文件系统,以便在下一个HTTP请求中访问所有缓存数据,使用Promise方式作为异步方法。

**系统能力**:SystemCapability.Communication.NetStack

L
liyufan 已提交
948
**返回值:**
L
liyufan 已提交
949 950 951

| 类型                              | 说明                                  |
| --------------------------------- | ------------------------------------- |
Y
Yangys 已提交
952
| Promise\<void\> | 以Promise形式返回写入结果。 |
L
liyufan 已提交
953 954 955 956

**示例:**

```js
957
httpResponseCache.flush().then(() => {
Y
Yangys 已提交
958
  console.info('flush success');
L
liyufan 已提交
959
}).catch(err => {
Y
Yangys 已提交
960
  console.info('flush fail');
L
liyufan 已提交
961 962 963 964 965
});
```

### delete<sup>9+</sup>

Y
Yangys 已提交
966
delete(callback: AsyncCallback\<void\>): void
L
liyufan 已提交
967 968 969 970 971 972 973 974 975

禁用缓存并删除其中的数据,使用callback方式作为异步方法。

**系统能力**:SystemCapability.Communication.NetStack

**参数:**

| 参数名   | 类型                                    | 必填 | 说明       |
| -------- | --------------------------------------- | ---- | ---------- |
Y
Yangys 已提交
976
| callback | AsyncCallback\<void\> | 是   | 回调函数返回删除结果。|
L
liyufan 已提交
977 978 979 980 981 982

**示例:**

```js
httpResponseCache.delete(err => {
  if (err) {
Y
Yangys 已提交
983
    console.info('delete fail');
L
liyufan 已提交
984 985
    return;
  }
Y
Yangys 已提交
986
  console.info('delete success');
L
liyufan 已提交
987 988 989 990
});
```
### delete<sup>9+</sup>

Y
Yangys 已提交
991
delete(): Promise\<void\>
L
liyufan 已提交
992 993 994 995 996

禁用缓存并删除其中的数据,使用Promise方式作为异步方法。

**系统能力**:SystemCapability.Communication.NetStack

L
liyufan 已提交
997
**返回值:**
L
liyufan 已提交
998 999 1000

| 类型                              | 说明                                  |
| --------------------------------- | ------------------------------------- |
Y
Yangys 已提交
1001
| Promise\<void\> | 以Promise形式返回删除结果。 |
L
liyufan 已提交
1002 1003 1004 1005

**示例:**

```js
1006
httpResponseCache.delete().then(() => {
Y
Yangys 已提交
1007
  console.info('delete success');
L
liyufan 已提交
1008
}).catch(err => {
Y
Yangys 已提交
1009
  console.info('delete fail');
L
liyufan 已提交
1010 1011 1012 1013 1014 1015 1016
});
```

## HttpDataType<sup>9+</sup>

http的数据类型。

Y
YOUR_NAME 已提交
1017 1018
**系统能力**:SystemCapability.Communication.NetStack

L
liyufan 已提交
1019
| 名称 | 值 | 说明     |
Y
Yangys 已提交
1020
| ------------------  | -- | ----------- |
L
liyufan 已提交
1021 1022 1023
| STRING              | 0 | 字符串类型。 |
| OBJECT              | 1 | 对象类型。    |
| ARRAY_BUFFER        | 2 | 二进制数组类型。|
L
liyufan 已提交
1024 1025 1026 1027 1028

## HttpProtocol<sup>9+</sup>

http协议版本。

Y
YOUR_NAME 已提交
1029 1030
**系统能力**:SystemCapability.Communication.NetStack

L
liyufan 已提交
1031 1032 1033 1034
| 名称  | 说明     |
| :-------- | :----------- |
| HTTP1_1   |  协议http1.1  |
| HTTP2     |  协议http2    |