• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.net.http (数据请求)
2
3<!--Kit: Network Kit-->
4<!--Subsystem: Communication-->
5<!--Owner: @wmyao_mm-->
6<!--Designer: @guo-min_net-->
7<!--Tester: @tongxilin-->
8<!--Adviser: @zhang_yixin13-->
9
10本模块提供HTTP数据请求能力。应用可以通过HTTP发起一个数据请求,支持常见的GET、POST、OPTIONS、HEAD、PUT、DELETE、TRACE、CONNECT方法。
11
12> **说明:**
13>
14> 本模块首批接口从API version 6开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
15## 导入模块
16
17```ts
18import { http } from '@kit.NetworkKit';
19```
20
21## 完整示例
22
23>**说明:**
24>
25>在本文档的示例中,通过this.context来获取UIAbilityContext,其中this代表继承自UIAbility的UIAbility实例。如需在页面中使用UIAbilityContext提供的能力,请参见[获取UIAbility的上下文信息](../../application-models/uiability-usage.md#获取uiability的上下文信息)。
26
27<!--code_no_check-->
28```ts
29// 引入包名
30import { http } from '@kit.NetworkKit';
31import { BusinessError } from '@kit.BasicServicesKit';
32import { common } from '@kit.AbilityKit';
33
34let context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
35// 每一个httpRequest对应一个HTTP请求任务,不可复用。
36let httpRequest = http.createHttp();
37// 用于订阅HTTP响应头,此接口会比request请求先返回。可以根据业务需要订阅此消息。
38// 从API 8开始,使用on('headersReceive', Callback)替代on('headerReceive', AsyncCallback)。 8+
39httpRequest.on('headersReceive', (header: Object) => {
40  console.info('header: ' + JSON.stringify(header));
41});
42
43httpRequest.request(// 填写HTTP请求的URL地址,可以带参数也可以不带参数。URL地址需要开发者自定义。请求的参数可以在extraData中指定。
44  "EXAMPLE_URL",
45  {
46    method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET47    // 当使用POST请求时此字段用于传递请求体内容,具体格式与服务端协商确定。
48    extraData: 'data to send',
49    expectDataType: http.HttpDataType.STRING, // 可选,指定返回数据的类型。
50    usingCache: true, // 可选,默认为true。
51    priority: 1, // 可选,默认为1。
52    // 开发者根据自身业务需要添加header字段,且header字段不支持传入map对象。
53    header: { 'Accept' : 'application/json' },
54    readTimeout: 60000, // 可选,默认为60000ms。
55    connectTimeout: 60000, // 可选,默认为60000ms。
56    usingProtocol: http.HttpProtocol.HTTP1_1, // 可选,协议类型默认值由系统自动指定。
57    usingProxy: false, //可选,默认不使用网络代理,自API 10开始支持该属性。
58    caPath: '/path/to/cacert.pem', // 可选,默认使用系统预设CA证书,自API 10开始支持该属性。
59    caData: '-----BEGIN CERTIFICATE-----\n' +
60        'MIIDaTCCAlGgAwIBAgIICN287lmB2cMwDQYJKoZIhvcNAQELBQAwgYoxCzAJBgNV\n' +
61        'BAYTAkNOMRMwEQYDVQQDDApleGFtcGxlLmNuMRAwDgYDVQQKDAdDb21wYW55MREw\n' +
62        'DwYDVQQLDAhEaXZpc2lvbjEOMAwGA1UECAwFQW5IdWkxDjAMBgNVBAcMBUhlRmVp\n' +
63        'MSEwHwYJKoZIhvcNAQkBFhJleGFtcGxlQGV4YW1wbGUuY24wHhcNMjUwNDEzMDAy\n' +
64        'MjQxWhcNMjgwNDEzMDAyMjQxWjBeMQswCQYDVQQGEwJDTjESMBAGA1UEAwwJMTI3\n' +
65        'LjAuMC4xMQkwBwYDVQQKDAAxCTAHBgNVBAsMADEJMAcGA1UECAwAMQkwBwYDVQQH\n' +
66        'DAAxDzANBgkqhkiG9w0BCQEWADCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC\n' +
67        'ggEBANN/JrQC8dy7sxUk+TDJlGlq4h8lajdqSASkFbWVBadU4eMCbRrKejXuFX/n\n' +
68        'Yu4J3wkgni0NKRejdWu/M+LLibQEIF9RUGNR/OgdlR4AKr8ZxmG44+7Ps2aiDcOy\n' +
69        'Z95UcxYj59ctfFk63cacbBi19aq200spjl/H0jTVsQ2/JvwMVEH62WbyjIJ3KXgq\n' +
70        'yyjf75rKbR9CdVdGk+OoR4S4c6nY5cTZP6T7iCupYR6MpKEtIR2bbams/N5GxQEh\n' +
71        '9+7YxswTQn4EkVhi+UOFZolYLhtIdoLThmStN+WiSL5VDvchAkTUmwUBTGV21WnH\n' +
72        'qo6J1t7XtwUpAZF6OuWl85R8D50CAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAqjKq\n' +
73        'gwR+4B6bwdAOZ6k0cutLqxvVgBaktX28omuYtoiYagM0zfB8/8WijXL8jT1VLEFx\n' +
74        'wPaojwegqYWANfQkPd7A6rjsabgOH7oYBCDoCH52cjzGlJunC0BL6w5g3z6MCOB4\n' +
75        'Ciz8rnYMvYqQJiMqrO7Po9onoFBHiRQGO4Wva3O8ErEmd2dKvXb3vN02P3T7CtwM\n' +
76        'Z6D0rtZbzdsSOQfGcX08WFIfvfpz6tdU/X/6VqKrt5oiaNQH7ded6gJ3C6RM/Q/x\n' +
77        'I2j/hSKy0yU7FoCFSOnlhxbm3TlbIvtjZKQ9ymK4x7iE0VKqExUAA6Z8qsIUBUt4\n' +
78        'aqNDeZWXFBqrSujLJA==\n' +
79        '-----END CERTIFICATE-----', // 可选,默认使用系统预设CA证书,自API 20开始支持该属性。
80    clientCert: { // 可选,默认不使用客户端证书,自API 11开始支持该属性。
81      certPath: '/path/to/client.pem', // 默认不使用客户端证书,自API 11开始支持该属性。
82      keyPath: '/path/to/client.key', // 若证书包含Key信息,传入空字符串,自API 11开始支持该属性。
83      certType: http.CertType.PEM, // 可选,默认使用PEM,自API 11开始支持该属性。
84      keyPassword: "passwordToKey" // 可选,输入key文件的密码,自API 11开始支持该属性。
85    },
86    certificatePinning: [ // 可选,支持证书锁定配置信息的动态设置,自API 12开始支持该属性。
87      {
88        publicKeyHash: 'Pin1', // 由应用传入的证书PIN码,自API 12开始支持该属性。
89        hashAlgorithm: 'SHA-256' // 加密算法,当前仅支持SHA-256,自API 12开始支持该属性。
90      }, {
91        publicKeyHash: 'Pin2', // 由应用传入的证书PIN码,自API 12开始支持该属性。
92        hashAlgorithm: 'SHA-256' // 加密算法,当前仅支持SHA-256,自API 12开始支持该属性。
93      }
94    ],
95    multiFormDataList: [ // 可选,仅当Header中,'content-Type'为'multipart/form-data'时生效,自API 11开始支持该属性。
96      {
97        name: "Part1", // 数据名,自API 11开始支持该属性。
98        contentType: 'text/plain', // 数据类型,自API 11开始支持该属性。
99        data: 'Example data', // 可选,数据内容,自API 11开始支持该属性。
100        remoteFileName: 'example.txt' // 可选,自API 11开始支持该属性。
101      }, {
102        name: "Part2", // 数据名,自API 11开始支持该属性。
103        contentType: 'text/plain', // 数据类型,自API 11开始支持该属性。
104        // data/app/el2/100/base/com.example.myapplication/haps/entry/files/fileName.txt
105        filePath: `${context.filesDir}/fileName.txt`, // 可选,传入文件路径,自API 11开始支持该属性。
106        remoteFileName: 'fileName.txt' // 可选,自API 11开始支持该属性。
107      }
108    ],
109    addressFamily: http.AddressFamily.DEFAULT // 可选,系统默认选择目标域名的IPv4地址或IPv6地址,自API 15开始支持该属性。
110  },
111  (err: BusinessError, data: http.HttpResponse) => {
112    if (!err) {
113      // data.result为HTTP响应内容,可根据业务需要进行解析。
114      console.info('Result:' + JSON.stringify(data.result));
115      console.info('code:' + JSON.stringify(data.responseCode));
116      console.info('type:' + JSON.stringify(data.resultType));
117      // data.header为HTTP响应头,可根据业务需要进行解析。
118      console.info('header:' + JSON.stringify(data.header));
119      console.info('cookies:' + JSON.stringify(data.cookies)); // 自API version 8开始支持cookie。
120      // 取消订阅HTTP响应头事件。
121      httpRequest.off('headersReceive');
122      // 当该请求使用完毕时,开发者务必调用destroy方法主动销毁该JavaScript Object。
123      httpRequest.destroy();
124    } else {
125      console.error('error:' + JSON.stringify(err));
126      // 取消订阅HTTP响应头事件。
127      httpRequest.off('headersReceive');
128      // 当该请求使用完毕时,开发者务必调用destroy方法主动销毁该JavaScript Object。
129      httpRequest.destroy();
130    }
131  });
132```
133
134> **说明:**
135> console.info()输出的数据中包含换行符会导致数据出现截断现象。
136>
137> 自API 12开始支持接收经过brotli算法压缩的HTTP响应。
138
139## http.createHttp
140
141createHttp(): HttpRequest
142
143创建一个HTTP请求,里面包括发起请求、中断请求、订阅/取消订阅HTTP Response Header事件。当发起多个HTTP请求时,需为每个HTTP请求创建对应HttpRequest对象。每一个HttpRequest对象对应一个HTTP请求。
144
145> **说明:**
146> 当该请求使用完毕时,需调用destroy方法主动销毁HttpRequest对象,否则会出现资源泄露问题。
147
148**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
149
150**系统能力**:SystemCapability.Communication.NetStack
151
152**返回值:**
153
154| 类型        | 说明                                                         |
155| :---------- | :----------------------------------------------------------- |
156| HttpRequest | 返回一个HttpRequest对象,里面包括request、requestInStream、destroy、on和off方法。 |
157
158**示例:**
159
160```ts
161import { http } from '@kit.NetworkKit';
162
163let httpRequest = http.createHttp();
164```
165
166## HttpRequest
167
168HTTP请求任务。在调用HttpRequest的方法前,需要先通过[createHttp()](#httpcreatehttp)创建一个任务。
169
170### request
171
172request(url: string, callback: AsyncCallback\<HttpResponse\>): void
173
174根据URL地址,发起HTTP网络请求,使用callback方式作为异步方法。
175
176> **说明:**
177>
178>(1) 此接口仅支持接收5MB以内的数据,如果需要接收超过5MB的数据,则需主动在[HttpRequestOptions](#httprequestoptions)的maxLimit中进行设置,或者使用[requestInStream](#requestinstream10)接口发起流式请求。<br>
179>(2) 如需传入cookies,请开发者自行在参数options中添加。<br>
180>(3) 若URL包含中文或其他语言,需先调用encodeURL(URL)编码,再发起请求。
181
182**需要权限**:ohos.permission.INTERNET
183
184**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
185
186**系统能力**:SystemCapability.Communication.NetStack
187
188**参数:**
189
190| 参数名   | 类型                                           | 必填 | 说明                    |
191| -------- | ---------------------------------------------- | ---- | ---------------------- |
192| url      | string                                         | 是   | 发起网络请求的URL地址。 |
193| callback | AsyncCallback\<[HttpResponse](#httpresponse)\> | 是   | 回调函数。    |
194
195**错误码:**
196
197以下错误码的详细介绍参见[通用错误码](../errorcode-universal.md)和[HTTP错误码](errorcode-net-http.md)。<br>
198HTTP错误码映射关系:2300000 + curl错误码。更多常用错误码可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)
199
200| 错误码ID   | 错误信息                                                         |
201|---------|----------------------------------------------------------------|
202| 401     | Parameter error.                                               |
203| 201     | Permission denied.                                             |
204| 2300001 | Unsupported protocol.                                          |
205| 2300003 | Invalid URL format or missing URL.                             |
206| 2300005 | Failed to resolve the proxy name.                              |
207| 2300006 | Failed to resolve the host name.                               |
208| 2300007 | Failed to connect to the server.                               |
209| 2300008 | Invalid server response.                                       |
210| 2300009 | Access to the remote resource denied.                          |
211| 2300016 | Error in the HTTP2 framing layer.                              |
212| 2300018 | Transferred a partial file.                                    |
213| 2300023 | Failed to write the received data to the disk or application.  |
214| 2300025 | Upload failed.                                                 |
215| 2300026 | Failed to open or read local data from the file or application.|
216| 2300027 | Out of memory.                                                 |
217| 2300028 | Operation timeout.                                             |
218| 2300047 | The number of redirections reaches the maximum allowed.        |
219| 2300052 | The server returned nothing (no header or data).               |
220| 2300055 | Failed to send data to the peer.                               |
221| 2300056 | Failed to receive data from the peer.                          |
222| 2300058 | Local SSL certificate error.                                   |
223| 2300059 | The specified SSL cipher cannot be used.                       |
224| 2300060 | Invalid SSL peer certificate or SSH remote key.                |
225| 2300061 | Invalid HTTP encoding format.                                  |
226| 2300063 | Maximum file size exceeded.                                    |
227| 2300070 | Remote disk full.                                              |
228| 2300073 | Remote file already exists.                                    |
229| 2300077 | The SSL CA certificate does not exist or is inaccessible.      |
230| 2300078 | Remote file not found.                                         |
231| 2300094 | Authentication error.                                          |
232| 2300997 | Cleartext traffic not permitted.                               |
233| 2300998 | It is not allowed to access this domain.                       |
234| 2300999 | Internal error.                                                 |
235
236
237**示例:**
238
239```ts
240import { http } from '@kit.NetworkKit';
241
242let httpRequest = http.createHttp();
243httpRequest.request("EXAMPLE_URL", (err: Error, data: http.HttpResponse) => {
244  if (!err) {
245    console.info('Result:' + data.result);
246    console.info('code:' + data.responseCode);
247    console.info('type:' + JSON.stringify(data.resultType));
248    console.info('header:' + JSON.stringify(data.header));
249    console.info('cookies:' + data.cookies); // 自API version 8开始支持cookie。
250  } else {
251    console.error('error:' + JSON.stringify(err));
252  }
253});
254```
255
256### request
257
258request(url: string, options: HttpRequestOptions, callback: AsyncCallback\<HttpResponse\>):void
259
260根据URL地址和相关配置项,发起HTTP网络请求,使用callback方式作为异步方法。
261
262> **说明:**
263>
264>(1) 此接口仅支持接收5MB以内的数据,如果需要接收超过5MB的数据,则需主动在[HttpRequestOptions](#httprequestoptions)的maxLimit中进行设置,或者使用[requestInStream](#requestinstream10)接口发起流式请求。<br>
265>(2) 如需传入cookies,请开发者自行在参数options中添加。<br>
266>(3) 若URL包含中文或其他语言,需先调用encodeURL(URL)编码,再发起请求。
267
268**需要权限**:ohos.permission.INTERNET
269
270**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
271
272**系统能力**:SystemCapability.Communication.NetStack
273
274**参数:**
275
276| 参数名   | 类型                                           | 必填 | 说明                                            |
277| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- |
278| url      | string                                         | 是   | 发起网络请求的URL地址。                         |
279| options  | HttpRequestOptions                             | 是   | 参考[HttpRequestOptions](#httprequestoptions)。 |
280| callback | AsyncCallback\<[HttpResponse](#httpresponse)\> | 是   | 回调函数。当请求成功时,回调内容是[HttpResponse](#httpresponse) ,请求失败时为undefined。                        |
281
282**错误码:**
283
284以下错误码的详细介绍参见[通用错误码](../errorcode-universal.md)和[HTTP错误码](errorcode-net-http.md)。<br>
285HTTP错误码映射关系:2300000 + curl错误码。更多常用错误码可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)
286
287| 错误码ID   | 错误信息                                                         |
288|---------|----------------------------------------------------------------|
289| 401     | Parameter error.                                               |
290| 201     | Permission denied.                                             |
291| 2300001 | Unsupported protocol.                                          |
292| 2300003 | Invalid URL format or missing URL.                             |
293| 2300005 | Failed to resolve the proxy name.                              |
294| 2300006 | Failed to resolve the host name.                               |
295| 2300007 | Failed to connect to the server.                               |
296| 2300008 | Invalid server response.                                       |
297| 2300009 | Access to the remote resource denied.                          |
298| 2300016 | Error in the HTTP2 framing layer.                              |
299| 2300018 | Transferred a partial file.                                    |
300| 2300023 | Failed to write the received data to the disk or application.  |
301| 2300025 | Upload failed.                                                 |
302| 2300026 | Failed to open or read local data from the file or application.|
303| 2300027 | Out of memory.                                                 |
304| 2300028 | Operation timeout.                                             |
305| 2300047 | The number of redirections reaches the maximum allowed.        |
306| 2300052 | The server returned nothing (no header or data).               |
307| 2300055 | Failed to send data to the peer.                               |
308| 2300056 | Failed to receive data from the peer.                          |
309| 2300058 | Local SSL certificate error.                                   |
310| 2300059 | The specified SSL cipher cannot be used.                       |
311| 2300060 | Invalid SSL peer certificate or SSH remote key.                |
312| 2300061 | Invalid HTTP encoding format.                                  |
313| 2300063 | Maximum file size exceeded.                                    |
314| 2300070 | Remote disk full.                                              |
315| 2300073 | Remote file already exists.                                    |
316| 2300077 | The SSL CA certificate does not exist or is inaccessible.      |
317| 2300078 | Remote file not found.                                         |
318| 2300094 | Authentication error.                                          |
319| 2300997 | Cleartext traffic not permitted.                               |
320| 2300998 | It is not allowed to access this domain.                       |
321| 2300999 | Internal error.                                                 |
322
323**示例:**
324
325```ts
326import { http } from '@kit.NetworkKit';
327
328class Header {
329  public contentType: string;
330
331  constructor(contentType: string) {
332    this.contentType = contentType;
333  }
334}
335
336let httpRequest = http.createHttp();
337let options: http.HttpRequestOptions = {
338    method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET339    // 当使用POST请求时此字段用于传递请求体内容,具体格式与服务端协商确定。
340    extraData: 'data to send',
341    expectDataType: http.HttpDataType.STRING, // 可选,指定返回数据的类型。
342    usingCache: true, // 可选,默认为true。
343    priority: 1, // 可选,默认为1。
344    // 开发者根据自身业务需要添加header字段。
345    header: new Header('application/json'),
346    readTimeout: 60000, // 可选,默认为60000ms。
347    connectTimeout: 60000, // 可选,默认为60000ms。
348    usingProtocol: http.HttpProtocol.HTTP1_1, // 可选,协议类型默认值由系统自动指定。
349    usingProxy: false, //可选,默认不使用网络代理,自API 10开始支持该属性。
350};
351
352httpRequest.request("EXAMPLE_URL", options, (err: Error, data: http.HttpResponse) => {
353  if (!err) {
354    console.info('Result:' + data.result);
355    console.info('code:' + data.responseCode);
356    console.info('type:' + JSON.stringify(data.resultType));
357    console.info('header:' + JSON.stringify(data.header));
358    console.info('cookies:' + data.cookies); // 自API version 8开始支持cookie。
359  } else {
360    console.error('error:' + JSON.stringify(err));
361  }
362});
363```
364
365### request
366
367request(url: string, options? : HttpRequestOptions): Promise\<HttpResponse\>
368
369根据URL地址,发起HTTP网络请求,使用Promise方式作为异步方法。
370
371> **说明:**
372>
373>(1) 此接口仅支持接收5MB以内的数据,如果需要接收超过5MB的数据,则需主动在[HttpRequestOptions](#httprequestoptions)的maxLimit中进行设置,或者使用[requestInStream](#requestinstream10)接口发起流式请求。<br>
374>(2) 如需传入cookies,请开发者自行在参数options中添加。<br>
375>(3) 若URL包含中文或其他语言,需先调用encodeURL(URL)编码,再发起请求。
376
377**需要权限**:ohos.permission.INTERNET
378
379**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
380
381**系统能力**:SystemCapability.Communication.NetStack
382
383**参数:**
384
385| 参数名  | 类型               | 必填 | 说明                                            |
386| ------- | ------------------ | ---- | ----------------------------------------------- |
387| url     | string             | 是   | 发起网络请求的URL地址。                         |
388| options | HttpRequestOptions | 否   | 参考[HttpRequestOptions](#httprequestoptions)。 |
389
390**返回值:**
391
392| 类型                                   | 说明                              |
393| :------------------------------------- | :-------------------------------- |
394| Promise<[HttpResponse](#httpresponse)> | 以Promise形式返回发起请求的结果。 |
395
396**错误码:**
397
398以下错误码的详细介绍参见[通用错误码](../errorcode-universal.md)和[HTTP错误码](errorcode-net-http.md)。<br>
399HTTP错误码映射关系:2300000 + curl错误码。更多常用错误码可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)
400
401| 错误码ID   | 错误信息                                                         |
402|---------|----------------------------------------------------------------|
403| 401     | Parameter error.                                               |
404| 201     | Permission denied.                                             |
405| 2300001 | Unsupported protocol.                                          |
406| 2300003 | Invalid URL format or missing URL.                             |
407| 2300005 | Failed to resolve the proxy name.                              |
408| 2300006 | Failed to resolve the host name.                               |
409| 2300007 | Failed to connect to the server.                               |
410| 2300008 | Invalid server response.                                       |
411| 2300009 | Access to the remote resource denied.                          |
412| 2300016 | Error in the HTTP2 framing layer.                              |
413| 2300018 | Transferred a partial file.                                    |
414| 2300023 | Failed to write the received data to the disk or application.  |
415| 2300025 | Upload failed.                                                 |
416| 2300026 | Failed to open or read local data from the file or application.|
417| 2300027 | Out of memory.                                                 |
418| 2300028 | Operation timeout.                                             |
419| 2300047 | The number of redirections reaches the maximum allowed.        |
420| 2300052 | The server returned nothing (no header or data).               |
421| 2300055 | Failed to send data to the peer.                               |
422| 2300056 | Failed to receive data from the peer.                          |
423| 2300058 | Local SSL certificate error.                                   |
424| 2300059 | The specified SSL cipher cannot be used.                       |
425| 2300060 | Invalid SSL peer certificate or SSH remote key.                |
426| 2300061 | Invalid HTTP encoding format.                                  |
427| 2300063 | Maximum file size exceeded.                                    |
428| 2300070 | Remote disk full.                                              |
429| 2300073 | Remote file already exists.                                    |
430| 2300077 | The SSL CA certificate does not exist or is inaccessible.      |
431| 2300078 | Remote file not found.                                         |
432| 2300094 | Authentication error.                                          |
433| 2300997 | Cleartext traffic not permitted.                               |
434| 2300998 | It is not allowed to access this domain.                       |
435| 2300999 | Internal error.                                                 |
436
437**示例:**
438
439```ts
440import { http } from '@kit.NetworkKit';
441
442class Header {
443  public contentType: string;
444
445  constructor(contentType: string) {
446    this.contentType = contentType;
447  }
448}
449
450let httpRequest = http.createHttp();
451let promise = httpRequest.request("EXAMPLE_URL", {
452  method: http.RequestMethod.GET,
453  connectTimeout: 60000,
454  readTimeout: 60000,
455  header: new Header('application/json')
456});
457promise.then((data:http.HttpResponse) => {
458  console.info('Result:' + data.result);
459  console.info('code:' + data.responseCode);
460  console.info('type:' + JSON.stringify(data.resultType));
461  console.info('header:' + JSON.stringify(data.header));
462  console.info('cookies:' + data.cookies); // 自API version 8开始支持cookie。
463  console.info('header.content-Type:' + data.header);
464  console.info('header.Status-Line:' + data.header);
465}).catch((err:Error) => {
466  console.error('error:' + JSON.stringify(err));
467});
468```
469
470### destroy
471
472destroy(): void
473
474中断请求任务。
475
476**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
477
478**系统能力**:SystemCapability.Communication.NetStack
479
480**示例:**
481
482```ts
483import { http } from '@kit.NetworkKit';
484let httpRequest = http.createHttp();
485
486httpRequest.destroy();
487```
488
489### requestInStream<sup>10+</sup>
490
491requestInStream(url: string, callback: AsyncCallback\<number\>): void
492
493根据URL地址,发起HTTP网络请求并返回流式响应,使用callback方式作为异步方法。
494
495**需要权限**:ohos.permission.INTERNET
496
497**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。
498
499**系统能力**:SystemCapability.Communication.NetStack
500
501**参数:**
502
503| 参数名   | 类型                                           | 必填 | 说明                                            |
504| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- |
505| url      | string                                         | 是   | 发起网络请求的URL地址。                         |
506| callback | AsyncCallback\<number\>       | 是   | 回调函数。                                      |
507
508**错误码:**
509
510以下错误码的详细介绍参见[通用错误码](../errorcode-universal.md)和[HTTP错误码](errorcode-net-http.md)。<br>
511HTTP错误码映射关系:2300000 + curl错误码。更多常用错误码可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)
512
513| 错误码ID   | 错误信息                                                         |
514|---------|----------------------------------------------------------------|
515| 401     | Parameter error.                                               |
516| 201     | Permission denied.                                             |
517| 2300001 | Unsupported protocol.                                          |
518| 2300003 | Invalid URL format or missing URL.                             |
519| 2300005 | Failed to resolve the proxy name.                              |
520| 2300006 | Failed to resolve the host name.                               |
521| 2300007 | Failed to connect to the server.                               |
522| 2300008 | Invalid server response.                                       |
523| 2300009 | Access to the remote resource denied.                          |
524| 2300016 | Error in the HTTP2 framing layer.                              |
525| 2300018 | Transferred a partial file.                                    |
526| 2300023 | Failed to write the received data to the disk or application.  |
527| 2300025 | Upload failed.                                                 |
528| 2300026 | Failed to open or read local data from the file or application.|
529| 2300027 | Out of memory.                                                 |
530| 2300028 | Operation timeout.                                             |
531| 2300047 | The number of redirections reaches the maximum allowed.        |
532| 2300052 | The server returned nothing (no header or data).               |
533| 2300055 | Failed to send data to the peer.                               |
534| 2300056 | Failed to receive data from the peer.                          |
535| 2300058 | Local SSL certificate error.                                   |
536| 2300059 | The specified SSL cipher cannot be used.                       |
537| 2300060 | Invalid SSL peer certificate or SSH remote key.                |
538| 2300061 | Invalid HTTP encoding format.                                  |
539| 2300063 | Maximum file size exceeded.                                    |
540| 2300070 | Remote disk full.                                              |
541| 2300073 | Remote file already exists.                                    |
542| 2300077 | The SSL CA certificate does not exist or is inaccessible.      |
543| 2300078 | Remote file not found.                                         |
544| 2300094 | Authentication error.                                          |
545| 2300997 | Cleartext traffic not permitted.                               |
546| 2300998 | It is not allowed to access this domain.                       |
547| 2300999 | Internal error.                                                 |
548
549**示例:**
550
551```ts
552import { http } from '@kit.NetworkKit';
553import { BusinessError } from '@kit.BasicServicesKit';
554
555let httpRequest = http.createHttp();
556httpRequest.requestInStream("EXAMPLE_URL", (err: BusinessError, data: number) => {
557  if (!err) {
558    console.info("requestInStream OK! ResponseCode is " + JSON.stringify(data));
559  } else {
560    console.error("requestInStream ERROR : err = " + JSON.stringify(err));
561  }
562})
563```
564
565### requestInStream<sup>10+</sup>
566
567requestInStream(url: string, options: HttpRequestOptions, callback: AsyncCallback\<number\>): void
568
569根据URL地址和相关配置项,发起HTTP网络请求并返回流式响应,使用callback方式作为异步方法。
570
571**需要权限**:ohos.permission.INTERNET
572
573**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。
574
575**系统能力**:SystemCapability.Communication.NetStack
576
577**参数:**
578
579| 参数名   | 类型                                           | 必填 | 说明                                            |
580| -------- | ---------------------------------------------- | ---- | ----------------------------------------------- |
581| url      | string                                         | 是   | 发起网络请求的URL地址。                         |
582| options  | HttpRequestOptions                             | 是   | 参考[HttpRequestOptions](#httprequestoptions)。 |
583| callback | AsyncCallback\<[number](#responsecode)\>       | 是   | 回调函数。                                      |
584
585**错误码:**
586
587以下错误码的详细介绍参见[通用错误码](../errorcode-universal.md)和[HTTP错误码](errorcode-net-http.md)。<br>
588HTTP错误码映射关系:2300000 + curl错误码。更多常用错误码可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)
589
590| 错误码ID   | 错误信息                                                         |
591|---------|----------------------------------------------------------------|
592| 401     | Parameter error.                                               |
593| 201     | Permission denied.                                             |
594| 2300001 | Unsupported protocol.                                          |
595| 2300003 | Invalid URL format or missing URL.                             |
596| 2300005 | Failed to resolve the proxy name.                              |
597| 2300006 | Failed to resolve the host name.                               |
598| 2300007 | Failed to connect to the server.                               |
599| 2300008 | Invalid server response.                                       |
600| 2300009 | Access to the remote resource denied.                          |
601| 2300016 | Error in the HTTP2 framing layer.                              |
602| 2300018 | Transferred a partial file.                                    |
603| 2300023 | Failed to write the received data to the disk or application.  |
604| 2300025 | Upload failed.                                                 |
605| 2300026 | Failed to open or read local data from the file or application.|
606| 2300027 | Out of memory.                                                 |
607| 2300028 | Operation timeout.                                             |
608| 2300047 | The number of redirections reaches the maximum allowed.        |
609| 2300052 | The server returned nothing (no header or data).               |
610| 2300055 | Failed to send data to the peer.                               |
611| 2300056 | Failed to receive data from the peer.                          |
612| 2300058 | Local SSL certificate error.                                   |
613| 2300059 | The specified SSL cipher cannot be used.                       |
614| 2300060 | Invalid SSL peer certificate or SSH remote key.                |
615| 2300061 | Invalid HTTP encoding format.                                  |
616| 2300063 | Maximum file size exceeded.                                    |
617| 2300070 | Remote disk full.                                              |
618| 2300073 | Remote file already exists.                                    |
619| 2300077 | The SSL CA certificate does not exist or is inaccessible.      |
620| 2300078 | Remote file not found.                                         |
621| 2300094 | Authentication error.                                          |
622| 2300997 | Cleartext traffic not permitted.                               |
623| 2300998 | It is not allowed to access this domain.                       |
624| 2300999 | Internal error.                                                 |
625
626**示例:**
627
628```ts
629import { http } from '@kit.NetworkKit';
630import { BusinessError } from '@kit.BasicServicesKit';
631
632class Header {
633  public contentType: string;
634
635  constructor(contentType: string) {
636    this.contentType = contentType;
637  }
638}
639
640let httpRequest = http.createHttp();
641let options: http.HttpRequestOptions = {
642    method: http.RequestMethod.POST, // 可选,默认为http.RequestMethod.GET643    // 当使用POST请求时此字段用于传递请求体内容,具体格式与服务端协商确定。
644    extraData: 'data to send',
645    expectDataType: http.HttpDataType.STRING, // 可选,指定返回数据的类型。
646    usingCache: true, // 可选,默认为true。
647    priority: 1, // 可选,默认为1。
648    // 开发者根据自身业务需要添加header字段。
649    header: new Header('application/json'),
650    readTimeout: 60000, // 可选,默认为60000ms。
651    connectTimeout: 60000, // 可选,默认为60000ms。
652    usingProtocol: http.HttpProtocol.HTTP1_1, // 可选,协议类型默认值由系统自动指定。
653    usingProxy: false, //可选,默认不使用网络代理,自API 10开始支持该属性。
654};
655httpRequest.requestInStream("EXAMPLE_URL", options, (err: BusinessError<void> , data: number) => {
656  if (!err) {
657    console.info("requestInStream OK! ResponseCode is " + JSON.stringify(data));
658  } else {
659    console.error("requestInStream ERROR : err = " + JSON.stringify(err));
660  }
661})
662```
663
664### requestInStream<sup>10+</sup>
665
666requestInStream(url: string, options? : HttpRequestOptions): Promise\<number\>
667
668根据URL地址,发起HTTP网络请求并返回流式响应,使用Promise方式作为异步方法。
669
670**需要权限**:ohos.permission.INTERNET
671
672**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。
673
674**系统能力**:SystemCapability.Communication.NetStack
675
676**参数:**
677
678| 参数名  | 类型               | 必填 | 说明                                            |
679| ------- | ------------------ | ---- | ----------------------------------------------- |
680| url     | string             | 是   | 发起网络请求的URL地址。                         |
681| options | HttpRequestOptions | 否   | 参考[HttpRequestOptions](#httprequestoptions)。 |
682
683**返回值:**
684
685| 类型                                   | 说明                              |
686| :------------------------------------- | :-------------------------------- |
687| Promise\<[number](#responsecode)\> | 以Promise形式返回发起请求的结果。 |
688
689**错误码:**
690
691以下错误码的详细介绍参见[通用错误码](../errorcode-universal.md)和[HTTP错误码](errorcode-net-http.md)。<br>
692HTTP错误码映射关系:2300000 + curl错误码。更多常用错误码可参考:[curl错误码](https://curl.se/libcurl/c/libcurl-errors.html)
693
694| 错误码ID   | 错误信息                                                         |
695|---------|----------------------------------------------------------------|
696| 401     | Parameter error.                                               |
697| 201     | Permission denied.                                             |
698| 2300001 | Unsupported protocol.                                          |
699| 2300003 | Invalid URL format or missing URL.                             |
700| 2300005 | Failed to resolve the proxy name.                              |
701| 2300006 | Failed to resolve the host name.                               |
702| 2300007 | Failed to connect to the server.                               |
703| 2300008 | Invalid server response.                                       |
704| 2300009 | Access to the remote resource denied.                          |
705| 2300016 | Error in the HTTP2 framing layer.                              |
706| 2300018 | Transferred a partial file.                                    |
707| 2300023 | Failed to write the received data to the disk or application.  |
708| 2300025 | Upload failed.                                                 |
709| 2300026 | Failed to open or read local data from the file or application.|
710| 2300027 | Out of memory.                                                 |
711| 2300028 | Operation timeout.                                             |
712| 2300047 | The number of redirections reaches the maximum allowed.        |
713| 2300052 | The server returned nothing (no header or data).               |
714| 2300055 | Failed to send data to the peer.                               |
715| 2300056 | Failed to receive data from the peer.                          |
716| 2300058 | Local SSL certificate error.                                   |
717| 2300059 | The specified SSL cipher cannot be used.                       |
718| 2300060 | Invalid SSL peer certificate or SSH remote key.                |
719| 2300061 | Invalid HTTP encoding format.                                  |
720| 2300063 | Maximum file size exceeded.                                    |
721| 2300070 | Remote disk full.                                              |
722| 2300073 | Remote file already exists.                                    |
723| 2300077 | The SSL CA certificate does not exist or is inaccessible.      |
724| 2300078 | Remote file not found.                                         |
725| 2300094 | Authentication error.                                          |
726| 2300997 | Cleartext traffic not permitted.                               |
727| 2300998 | It is not allowed to access this domain.                       |
728| 2300999 | Internal error.                                                 |
729
730**示例:**
731
732```ts
733import { http } from '@kit.NetworkKit';
734
735class Header {
736  public contentType: string;
737
738  constructor(contentType: string) {
739    this.contentType = contentType;
740  }
741}
742
743let httpRequest = http.createHttp();
744let promise = httpRequest.requestInStream("EXAMPLE_URL", {
745  method: http.RequestMethod.GET,
746  connectTimeout: 60000,
747  readTimeout: 60000,
748  header: new Header('application/json')
749});
750promise.then((data: number) => {
751  console.info("requestInStream OK!" + data);
752}).catch((err: Error) => {
753  console.error("requestInStream ERROR : err = " + JSON.stringify(err));
754});
755```
756
757### on("headerReceive")<sup>(deprecated)</sup>
758
759on(type: "headerReceive", callback: AsyncCallback\<Object\>): void
760
761订阅HTTP Response Header 事件。
762
763> **说明:**
764> 从API version 6开始支持,从API version 8开始废弃,建议使用[on("headersReceive")<sup>8+</sup>](#onheadersreceive8)替代。
765
766**系统能力**:SystemCapability.Communication.NetStack
767
768**参数:**
769
770| 参数名   | 类型                    | 必填 | 说明                              |
771| -------- | ----------------------- | ---- | --------------------------------- |
772| type     | string                  | 是   | 订阅的事件类型,'headerReceive'。 |
773| callback | AsyncCallback\<Object\> | 是   | 回调函数。当订阅成功,error为undefined,data为获取到HTTP响应头;否则为错误对象。 |
774
775**示例:**
776
777```ts
778import { http } from '@kit.NetworkKit';
779import { BusinessError } from '@kit.BasicServicesKit';
780
781let httpRequest = http.createHttp();
782httpRequest.on("headerReceive", (data: BusinessError) => {
783  console.error("error:" + JSON.stringify(data));
784});
785```
786
787### off("headerReceive")<sup>(deprecated)</sup>
788
789off(type: "headerReceive", callback?: AsyncCallback\<Object\>): void
790
791取消订阅HTTP Response Header事件。
792
793> **说明:**
794>
795> 从API version 6开始支持,从API version 8开始废弃,建议使用[off("headersReceive")<sup>8+</sup>](#offheadersreceive8)替代。
796
797**系统能力**:SystemCapability.Communication.NetStack
798
799**参数:**
800
801| 参数名   | 类型                    | 必填 | 说明                                  |
802| -------- | ----------------------- | ---- | ------------------------------------- |
803| type     | string                  | 是   | 取消订阅的事件类型,'headerReceive'。 |
804| callback | AsyncCallback\<Object\> | 否   | 回调函数。可以指定传入on中的callback取消对应的订阅,也可以不指定callback清空所有订阅。                           |
805
806**示例:**
807
808```ts
809import { http } from '@kit.NetworkKit';
810
811let httpRequest = http.createHttp();
812httpRequest.off("headerReceive");
813```
814
815### on("headersReceive")<sup>8+</sup>
816
817on(type: "headersReceive", callback: Callback\<Object\>): void
818
819订阅HTTP Response Header 事件。
820
821**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
822
823**系统能力**:SystemCapability.Communication.NetStack
824
825**参数:**
826
827| 参数名   | 类型               | 必填 | 说明                        |
828| -------- | ------------------ | ---- |---------------------------|
829| type     | string             | 是   | 订阅的事件类型:'headersReceive'。 |
830| callback | Callback\<Object\> | 是   | 回调函数,返回HTTP响应头对象。         |
831
832**示例:**
833
834```ts
835import { http } from '@kit.NetworkKit';
836
837let httpRequest = http.createHttp();
838httpRequest.on("headersReceive", (header: Object) => {
839  console.info("header: " + JSON.stringify(header));
840});
841httpRequest.off("headersReceive");
842```
843
844### off("headersReceive")<sup>8+</sup>
845
846off(type: "headersReceive", callback?: Callback\<Object\>): void
847
848取消订阅HTTP Response Header 事件。
849
850**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
851
852**系统能力**:SystemCapability.Communication.NetStack
853
854**参数:**
855
856| 参数名   | 类型               | 必填 | 说明                                   |
857| -------- | ------------------ | ---- | -------------------------------------- |
858| type     | string             | 是   | 取消订阅的事件类型:'headersReceive'。 |
859| callback | Callback\<Object\> | 否   | 回调函数。可以指定传入on中的callback取消对应的订阅,也可以不指定callback清空所有订阅。                             |
860
861**示例:**
862
863```ts
864import { http } from '@kit.NetworkKit';
865
866let httpRequest = http.createHttp();
867httpRequest.on("headersReceive", (header: Object) => {
868  console.info("header: " + JSON.stringify(header));
869});
870httpRequest.off("headersReceive");
871```
872
873### once("headersReceive")<sup>8+</sup>
874
875once(type: "headersReceive", callback: Callback\<Object\>): void
876
877订阅HTTP Response Header 事件,只能触发一次。触发之后,订阅器就会被移除。使用callback方式作为异步方法。
878
879**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。
880
881**系统能力**:SystemCapability.Communication.NetStack
882
883**参数:**
884
885| 参数名   | 类型               | 必填 | 说明                               |
886| -------- | ------------------ | ---- | ---------------------------------- |
887| type     | string             | 是   | 订阅的事件类型:'headersReceive'。 |
888| callback | Callback\<Object\> | 是   | 回调函数。返回HTTP响应头对象。                         |
889
890**示例:**
891
892```ts
893import { http } from '@kit.NetworkKit';
894
895let httpRequest = http.createHttp();
896httpRequest.once("headersReceive", (header: Object) => {
897  console.info("header: " + JSON.stringify(header));
898});
899```
900
901### on("dataReceive")<sup>10+</sup>
902
903on(type: "dataReceive", callback: Callback\<ArrayBuffer\>): void
904
905订阅HTTP流式响应数据接收事件。
906
907**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。
908
909**系统能力**:SystemCapability.Communication.NetStack
910
911**参数:**
912
913| 参数名   | 类型                    | 必填 | 说明                              |
914| -------- | ----------------------- | ---- | --------------------------------- |
915| type     | string                  | 是   | 订阅的事件类型,'dataReceive'。 |
916| callback | Callback\<ArrayBuffer\> | 是   | 回调函数。当订阅成功时,error为undefined,data为获取到的HTTP流式数据接收数据,类型为ArrayBuffer;否则为错误对象。 |
917
918**示例:**
919
920```ts
921import { http } from '@kit.NetworkKit';
922
923let httpRequest = http.createHttp();
924httpRequest.on("dataReceive", (data: ArrayBuffer) => {
925  console.info("dataReceive length: " + JSON.stringify(data.byteLength));
926});
927httpRequest.off("dataReceive");
928```
929
930### off("dataReceive")<sup>10+</sup>
931
932off(type: "dataReceive", callback?: Callback\<ArrayBuffer\>): void
933
934取消订阅HTTP流式响应数据接收事件。
935
936**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。
937
938**系统能力**:SystemCapability.Communication.NetStack
939
940**参数:**
941
942| 参数名   | 类型               | 必填 | 说明                                   |
943| -------- | ------------------ | ---- | -------------------------------------- |
944| type     | string             | 是   | 取消订阅的事件类型:'dataReceive'。 |
945| callback | Callback\<ArrayBuffer\> | 否   | 回调函数。可以指定传入on中的callback取消对应的订阅,也可以不指定callback清空所有订阅。                             |
946
947**示例:**
948
949```ts
950import { http } from '@kit.NetworkKit';
951
952let httpRequest = http.createHttp();
953httpRequest.on("dataReceive", (data: ArrayBuffer) => {
954  console.info("dataReceive length: " + JSON.stringify(data.byteLength));
955});
956httpRequest.off("dataReceive");
957```
958
959### on("dataEnd")<sup>10+</sup>
960
961on(type: "dataEnd", callback: Callback\<void\>): void
962
963订阅HTTP流式响应数据接收完毕事件。
964
965**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。
966
967**系统能力**:SystemCapability.Communication.NetStack
968
969**参数:**
970
971| 参数名   | 类型                    | 必填 | 说明                              |
972| -------- | ----------------------- | ---- | --------------------------------- |
973| type     | string                  | 是   | 订阅的事件类型,'dataEnd'。 |
974| callback | Callback\<void\>   | 是   | 回调函数。当订阅成功时,err为undefined,否则为错误对象。                        |
975
976**示例:**
977
978```ts
979import { http } from '@kit.NetworkKit';
980
981let httpRequest = http.createHttp();
982httpRequest.on("dataEnd", () => {
983  console.info("Receive dataEnd !");
984});
985httpRequest.off("dataEnd");
986```
987
988### off("dataEnd")<sup>10+</sup>
989
990off(type: "dataEnd", callback?: Callback\<void\>): void
991
992取消订阅HTTP流式响应数据接收完毕事件。
993
994**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。
995
996**系统能力**:SystemCapability.Communication.NetStack
997
998**参数:**
999
1000| 参数名   | 类型               | 必填 | 说明                                   |
1001| -------- | ------------------ | ---- | -------------------------------------- |
1002| type     | string             | 是   | 取消订阅的事件类型:'dataEnd'。 |
1003| callback | Callback\<void\>   | 否   | 回调函数。可以指定传入on中的callback取消对应的订阅,也可以不指定callback清空所有订阅。                             |
1004
1005**示例:**
1006
1007```ts
1008import { http } from '@kit.NetworkKit';
1009
1010let httpRequest = http.createHttp();
1011httpRequest.on("dataEnd", () => {
1012  console.info("Receive dataEnd !");
1013});
1014httpRequest.off("dataEnd");
1015```
1016
1017### on('dataReceiveProgress')<sup>10+</sup>
1018
1019on(type: 'dataReceiveProgress', callback: Callback\<DataReceiveProgressInfo\>): void
1020
1021订阅HTTP流式响应数据接收进度事件。
1022
1023**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。
1024
1025**系统能力**:SystemCapability.Communication.NetStack
1026
1027**参数:**
1028
1029| 参数名   | 类型                    | 必填 | 说明                              |
1030| -------- | ----------------------- | ---- | --------------------------------- |
1031| type     | string                  | 是   | 订阅的事件类型,'dataReceiveProgress'。 |
1032| callback | Callback\<[DataReceiveProgressInfo](#datareceiveprogressinfo11)\>   | 是   | 回调函数。当订阅成功时,回调内容是[DataReceiveProgressInfo](#datareceiveprogressinfo11),订阅失败时为undefined。 |
1033
1034**示例:**
1035
1036```ts
1037import { http } from '@kit.NetworkKit';
1038
1039let httpRequest = http.createHttp();
1040httpRequest.on("dataReceiveProgress", (data: http.DataReceiveProgressInfo) => {
1041  console.info("dataReceiveProgress:" + JSON.stringify(data));
1042});
1043httpRequest.off("dataReceiveProgress");
1044```
1045
1046### off('dataReceiveProgress')<sup>10+</sup>
1047
1048off(type: 'dataReceiveProgress', callback?: Callback\<DataReceiveProgressInfo\>): void
1049
1050取消订阅HTTP流式响应数据接收进度事件。
1051
1052**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。
1053
1054**系统能力**:SystemCapability.Communication.NetStack
1055
1056**参数:**
1057
1058| 参数名   | 类型               | 必填 | 说明                                   |
1059| -------- | ------------------ | ---- | -------------------------------------- |
1060| type     | string             | 是   | 取消订阅的事件类型:'dataReceiveProgress'。 |
1061| callback | Callback\<[DataReceiveProgressInfo](#datareceiveprogressinfo11)\>   | 否   | 回调函数。 可以指定传入on中的callback取消对应的订阅,也可以不指定callback清空所有订阅。    |
1062
1063**示例:**
1064
1065```ts
1066import { http } from '@kit.NetworkKit';
1067
1068let httpRequest = http.createHttp();
1069httpRequest.on("dataReceiveProgress", (data: http.DataReceiveProgressInfo) => {
1070  console.info("dataReceiveProgress:" + JSON.stringify(data));
1071});
1072httpRequest.off("dataReceiveProgress");
1073```
1074
1075### on('dataSendProgress')<sup>11+</sup>
1076
1077on(type: 'dataSendProgress', callback: Callback\<DataSendProgressInfo\>): void
1078
1079订阅HTTP网络请求数据发送进度事件。
1080
1081**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。
1082
1083**系统能力**:SystemCapability.Communication.NetStack
1084
1085**参数:**
1086
1087| 参数名   | 类型                    | 必填 | 说明                              |
1088| -------- | ----------------------- | ---- | --------------------------------- |
1089| type     | string                  | 是   | 订阅的事件类型,'dataSendProgress'。 |
1090| callback | Callback\<[DataSendProgressInfo](#datasendprogressinfo11)\>   | 是   | 回调函数。当订阅成功时,回调内容是[DataSendProgressInfo](#datasendprogressinfo11),订阅失败时为undefined。|
1091
1092**示例:**
1093
1094```ts
1095import { http } from '@kit.NetworkKit';
1096
1097let httpRequest = http.createHttp();
1098httpRequest.on("dataSendProgress", (data: http.DataSendProgressInfo) => {
1099  console.info("dataSendProgress:" + JSON.stringify(data));
1100});
1101httpRequest.off("dataSendProgress");
1102```
1103
1104### off('dataSendProgress')<sup>11+</sup>
1105
1106off(type: 'dataSendProgress', callback?: Callback\<DataSendProgressInfo\>): void
1107
1108取消订阅HTTP网络请求数据发送进度事件。
1109
1110**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。
1111
1112**系统能力**:SystemCapability.Communication.NetStack
1113
1114**参数:**
1115
1116| 参数名   | 类型               | 必填 | 说明                                   |
1117| -------- | ------------------ | ---- | -------------------------------------- |
1118| type     | string             | 是   | 取消订阅的事件类型:'dataSendProgress'。 |
1119| callback | Callback\<[DataSendProgressInfo](#datasendprogressinfo11)\>  | 否 | 回调函数。可以指定传入on中的callback取消对应的订阅,也可以不指定callback清空所有订阅。 |
1120
1121**示例:**
1122
1123```ts
1124import { http } from '@kit.NetworkKit';
1125
1126let httpRequest = http.createHttp();
1127httpRequest.on("dataSendProgress", (data: http.DataSendProgressInfo) => {
1128  console.info("dataSendProgress:" + JSON.stringify(data));
1129});
1130httpRequest.off("dataSendProgress");
1131```
1132
1133## HttpRequestOptions
1134
1135发起HTTP请求时,可选配置信息。
1136
1137**系统能力**:SystemCapability.Communication.NetStack
1138
1139| 名称         | 类型                                          | 只读 | 可选 | 说明                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
1140| -------------- | --------------------------------------------- | ---- |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
1141| method         | [RequestMethod](#requestmethod)               | 否  | 是  | 请求方式,默认为GET。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
1142| extraData      | string \| Object \| ArrayBuffer | 否  | 是  | 发送请求的额外数据,默认无此字段。<br />**说明:** 没有额外数据时,避免添加该参数;若必须添加,请填写undefined或者null,避免直接传入"。<br />1. 当HTTP请求为POST、PUT等方法时,此字段为HTTP请求的content,以UTF-8编码形式作为请求体。<br />示例如下:<br />  (1) 当'content-Type'为'application/x-www-form-urlencoded'时,请求提交的信息主体数据必须在key和value进行URL转码后(encodeURIComponent/encodeURI),按照键值对"key1=value1&key2=value2&key3=value3"的方式进行编码,该字段对应的类型通常为String。<br />(2) 当'content-Type'为'text/xml'时,该字段对应的类型通常为String。<br />(3) 当'content-Type'为'application/json'时,该字段对应的类型通常为Object。<br />(4) 当'content-Type'为'application/octet-stream'时,该字段对应的类型通常为ArrayBuffer。<br />(5) 当'content-Type'为'multipart/form-data'且需上传的字段为文件时,该字段对应的类型通常为ArrayBuffer。<br>以上信息仅供参考,并可能根据具体情况有所不同。<br />2. 当HTTP请求为GET、OPTIONS、DELETE、TRACE、CONNECT等方法时,此字段为HTTP请求参数的补充。开发者需传入Encode编码后的string类型参数,Object类型的参数无需预编码,参数内容会拼接到URL中进行发送。ArrayBuffer类型的参数不会做拼接处理。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
1143| expectDataType<sup>9+</sup>  | [HttpDataType](#httpdatatype9)  | 否  | 是  | 指定返回数据的类型,默认无此字段。如果设置了此参数,系统将优先返回指定的类型。当指定其类型为Object时,最大长度为65536字符数。 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
1144| usingCache<sup>9+</sup>      | boolean                         | 否  | 是  | 是否使用缓存,true表示请求时优先读取缓存,false表示不使用缓存;默认为true,请求时优先读取缓存。缓存跟随当前进程生效,新缓存会替换旧缓存。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
1145| priority<sup>9+</sup>        | number                          | 否  | 是  | HTTP/HTTPS请求并发优先级,值越大优先级越高,范围[1,1000],默认为1。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   |
1146| header                       | Object                          | 否  | 是  | HTTP请求头字段。当请求方式为"POST" "PUT" "DELETE" 或者""时,默认{'content-Type': 'application/json'}, 否则默认{'content-Type': 'application/x-www-form-urlencoded'}。<br />如果head中包含number类型的字段,最大支持int64的整数。<br />header字段不支持传入map对象。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
1147| readTimeout                  | number                          | 否  | 是  | 读取超时时间。单位为毫秒(ms),默认为60000ms。传入值需为uint32_t范围内的整数。<br />设置为0表示不会出现超时情况。 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
1148| connectTimeout               | number                          | 否  | 是  | 连接超时时间。单位为毫秒(ms),默认为60000ms。传入值需为uint32_t范围内的整数。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
1149| usingProtocol<sup>9+</sup>   | [HttpProtocol](#httpprotocol9)  | 否  | 是  | 使用协议。默认值由系统自动指定。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
1150| usingProxy<sup>10+</sup>     | boolean \| [HttpProxy](js-apis-net-connection.md#httpproxy10)               | 否  | 是  | HTTP代理配置,该项不配置时表示不使用代理。<br />- 当usingProxy为布尔类型true时,使用默认网络代理,为false时,不使用代理。<br />- 当usingProxy为HttpProxy类型时,使用指定网络代理。当前HttpProxy不支持指定username和password字段。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
1151| caPath<sup>10+</sup>     | string               | 否  | 是  | 如果设置了此参数,系统将使用用户指定路径的CA证书(开发者需保证该路径下CA证书的可访问性),否则将使用系统预设CA证书。<br />系统预设CA证书位置:/etc/ssl/certs/cacert.pem。证书路径为沙箱映射路径(开发者可通过UIAbilityContext提供的能力获取应用沙箱路径)。目前仅支持后缀名为.pem的文本格式证书。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
1152| caData<sup>20+</sup>     | string               | 否  | 是  | 如果设置了此参数且证书有效,系统将使用用户指定的CA证书和系统预设的CA证书;否则仅使用系统预设的CA证书。如果同时设置了caPath和caData,caData将被系统忽略。目前仅支持传入.pem格式的证书内容,最大长度为8000字节。仅支持传入单证书,不支持证书链传入。<br />系统预设CA证书位置:/etc/ssl/certs/cacert.pem。证书路径为沙箱映射路径(开发者可通过UIAbilityContext提供的能力获取应用沙箱路径)。<br>**原子化服务API:** 从API version 20开始,该接口支持在原子化服务中使用。|
1153| resumeFrom<sup>11+</sup> | number | 否 | 是 | 用于设置下载起始位置,该参数只能用于GET方法,不能用于其他。HTTP标准(RFC 7233第3.1节)允许服务器忽略范围请求。<br />- 使用HTTP PUT时,不能使用该选项,因为该选项可能与其他选项冲突。<br />- 取值范围是:[1,4294967296(4GB)],超出范围则不生效。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
1154| resumeTo<sup>11+</sup> | number | 否 | 是 | 用于设置下载结束位置,该参数只能用于GET方法,不能用于其他。HTTP标准(RFC 7233第3.1节)允许服务器忽略范围请求。<br />- 使用HTTP PUT时,不能使用该选项,因为该选项可能与其他选项冲突。<br />- 取值范围是:[1,4294967296(4GB)],超出范围则不生效。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
1155| clientCert<sup>11+</sup> | [ClientCert](#clientcert11) | 否 | 是 | 支持传输客户端证书。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
1156| dnsOverHttps<sup>11+</sup> | string | 否 | 是 | 设置使用HTTPS协议的服务器进行DNS解析。<br />- 参数必须以以下格式进行URL编码:"https:// host:port/path"。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
1157| dnsServers<sup>11+</sup> | Array\<string\> | 否 | 是 | 设置指定的DNS服务器进行DNS解析。<br />- 最多可以设置3个DNS解析服务器。如果有3个以上,只取前3个。<br />- 服务器必须是IPV4或者IPV6地址。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
1158| maxLimit<sup>11+</sup>   | number   | 否 | 是 | 响应消息的最大字节限制。<br />默认值为5\*1024\*1024,以字节为单位。最大值为100\*1024\*1024,以字节为单位。<br />                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
1159| multiFormDataList<sup>11+</sup> | Array<[MultiFormData](#multiformdata11)> | 否 | 是 | 当'content-Type'为'multipart/form-data'时,则上传该字段定义的数据字段表单列表。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
1160| certificatePinning<sup>12+</sup> | [CertificatePinning](#certificatepinning12) \| CertificatePinning[] | 否 | 是 | 支持动态设置证书锁定配置,可以传入单个或多个证书PIN码。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    |
1161| addressFamily<sup>15+</sup> | [AddressFamily](#addressfamily15) | 否 | 是 | 支持解析目标域名时限定地址类型。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 |
1162| remoteValidation<sup>18+</sup> | [RemoteValidation](#remotevalidation18)                             | 否 | 是 | 证书颁发机构(CA),用于验证远程服务器的身份。如果未设置此字段,系统CA将用于验证远程服务器的标识。<br>**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
1163| tlsOptions<sup>18+</sup> | [TlsOptions](#tlsoptions18)                                         | 否 | 是 | TLS配置。<br>**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        |
1164| serverAuthentication<sup>18+</sup> | [ServerAuthentication](#serverauthentication18)                     | 否 | 是 | 安全连接期间的服务器身份验证配置。默认不认证。<br>**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       |
1165| sslType<sup>20+</sup> | [SslType](#ssltype20) | 否 | 是 | 使用安全通信协议TLS(默认)或TLCP。如果使用TLCP,则所有与TLS相关的选项(如caPath、caData和clientCert)都将被忽略。<br>**原子化服务API:** 从API version 20开始,该接口支持在原子化服务中使用。 |
1166| clientEncCert<sup>20+</sup> | [ClientCert](#clientcert11) | 否 | 是 | 支持应用程序传入客户端证书,使服务器能够进行验证客户端的加密身份。<br>**原子化服务API:** 从API version 20开始,该接口支持在原子化服务中使用。 |
1167
1168## RequestMethod
1169
1170HTTP 请求方法。
1171
1172**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
1173
1174**系统能力**:SystemCapability.Communication.NetStack
1175
1176| 名称    | 值      | 说明                |
1177| :------ | ------- | :------------------ |
1178| OPTIONS | "OPTIONS" | OPTIONS方法描述了目标资源的通信选项。 |
1179| GET     | "GET"     | GET方法请求指定资源的表示。使用GET的请求应该只检索数据,不应该包含请求内容。 |
1180| HEAD    | "HEAD"    | HEAD方法请求与GET请求相同的响应,但没有响应主体。 |
1181| POST    | "POST"    | POST方法将实体提交给指定的资源,通常会导致服务器上的状态更改。 |
1182| PUT     | "PUT"     | PUT方法将目标资源的所有当前表示替换为请求内容。 |
1183| DELETE  | "DELETE"  | DELETE方法用于删除指定的资源。 |
1184| TRACE   | "TRACE"   | TRACE方法沿到达目标资源的路径执行消息环回测试。 |
1185| CONNECT | "CONNECT" | CONNECT方法建立到由目标资源标识的服务器的隧道。 |
1186
1187## ResponseCode
1188
1189发起请求返回的响应码。
1190
1191**系统能力**:SystemCapability.Communication.NetStack
1192
1193| 名称              | 值   | 说明                                                         |
1194| ----------------- | ---- | ------------------------------------------------------------ |
1195| OK                | 200  | 请求成功。用于GET与POST请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                            |
1196| CREATED           | 201  | 已创建。请求成功并已创建新资源。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                           |
1197| ACCEPTED          | 202  | 已接受。请求已被接受,但未处理完成。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                         |
1198| NOT_AUTHORITATIVE | 203  | 非授权信息。请求成功。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                       |
1199| NO_CONTENT        | 204  | 无内容。服务器成功处理,但未返回内容。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                       |
1200| RESET             | 205  | 重置内容。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                                   |
1201| PARTIAL           | 206  | 部分内容。服务器成功处理了部分GET请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                      |
1202| MULT_CHOICE       | 300  | 多种选择。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                                   |
1203| MOVED_PERM        | 301  | 永久移动。请求的资源已被永久的移动到新URI,返回信息会包括新的URI,浏览器会自动定向到新URI。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
1204| MOVED_TEMP        | 302  | 临时移动。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                                   |
1205| SEE_OTHER         | 303  | 查看其它地址。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                               |
1206| NOT_MODIFIED      | 304  | 未修改。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                                     |
1207| USE_PROXY         | 305  | 使用代理。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                                   |
1208| BAD_REQUEST       | 400  | 客户端请求的语法错误,服务器无法理解。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                       |
1209| UNAUTHORIZED      | 401  | 请求需要用户的身份认证。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                     |
1210| PAYMENT_REQUIRED  | 402  | 保留字段,将来使用。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                             |
1211| FORBIDDEN         | 403  | 服务器理解请求客户端的请求,但是拒绝执行此请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。             |
1212| NOT_FOUND         | 404  | 服务器无法根据客户端的请求找到资源(网页)。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                 |
1213| BAD_METHOD        | 405  | 客户端请求中的方法被禁止。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                   |
1214| NOT_ACCEPTABLE    | 406  | 服务器无法根据客户端请求的内容特性完成请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                 |
1215| PROXY_AUTH        | 407  | 请求需要代理的身份认证。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                     |
1216| CLIENT_TIMEOUT    | 408  | 请求超时。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                         |
1217| CONFLICT          | 409  | 服务器完成客户端的PUT请求时可能返回此代码,服务器处理请求时发生了冲突。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
1218| GONE              | 410  | 客户端请求的资源已经不存在。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                 |
1219| LENGTH_REQUIRED   | 411  | 服务器无法处理客户端发送的不带Content-Length的请求信息。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。     |
1220| PRECON_FAILED     | 412  | 客户端请求信息的先决条件错误。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                               |
1221| ENTITY_TOO_LARGE  | 413  | 由于请求的实体过大,服务器无法处理,因此拒绝请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。           |
1222| REQ_TOO_LONG      | 414  | 请求的URI过长(URI通常为网址),服务器无法处理。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。             |
1223| UNSUPPORTED_TYPE  | 415  | 服务器无法处理请求的格式。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                   |
1224| RANGE_NOT_SATISFIABLE<sup>12+</sup> | 416  | 请求范围不符合要求。<br>**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。                                  |
1225| INTERNAL_ERROR    | 500  | 服务器内部错误,无法完成请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                               |
1226| NOT_IMPLEMENTED   | 501  | 服务器不支持请求的功能,无法完成请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                       |
1227| BAD_GATEWAY       | 502  | 充当网关或代理的服务器,从远端服务器接收到了一个无效的请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
1228| UNAVAILABLE       | 503  | 由于超载或系统维护,服务器暂时无法处理客户端的请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。       |
1229| GATEWAY_TIMEOUT   | 504  | 充当网关或代理的服务器,未及时从远端服务器获取请求。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。         |
1230| VERSION           | 505  | 服务器请求的HTTP协议的版本。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                                 |
1231
1232## HttpResponse
1233
1234request方法回调函数的返回值类型。
1235
1236**系统能力**:SystemCapability.Communication.NetStack
1237
1238| 名称                 | 类型                                         | 必填 | 说明                                                          |
1239| -------------------- | -------------------------------------------- | ---- | ------------------------------------------------------------ |
1240| result               | string \| Object \| ArrayBuffer | 是   | HTTP请求根据响应头中content-type类型返回对应的响应格式内容,若HttpRequestOptions无expectDataType字段,按如下规则返回:<br />- application/json:返回JSON格式的字符串。<br />- application/octet-stream:ArrayBuffer。<br />- image:ArrayBuffer。<br />- 其他:string。<br /> 若HttpRequestOption有expectDataType字段,开发者需传入与服务器返回类型相同的数据类型。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
1241| resultType<sup>9+</sup> | [HttpDataType](#httpdatatype9)             | 是   | 返回值类型。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。                           |
1242| responseCode         | [ResponseCode](#responsecode) \| number      | 是   | 回调函数执行成功时,此字段为[ResponseCode](#responsecode)。若执行失败,错误码将会从AsyncCallback中的err字段返回。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
1243| 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']。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。 |
1244| cookies<sup>8+</sup> | string                                       | 是   | 服务器返回的原始cookies。开发者可自行处理。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。               |
1245| performanceTiming<sup>11+</sup> | [PerformanceTiming](#performancetiming11) | 是 | HTTP请求的各个阶段的耗时。|
1246
1247## ClientCert<sup>11+</sup>
1248
1249客户端证书类型。
1250
1251**系统能力**:SystemCapability.Communication.NetStack
1252
1253| 名称 | 类型 | 必填 | 说明 |
1254| -------- | -------| --- | ----------- |
1255| certPath | string | 是 | 证书路径。 |
1256| certType | [CertType](#certtype11) | 否 | 证书类型,默认是PEM。 |
1257| keyPath | string | 是 | 证书密钥的路径。 |
1258| keyPassword | string | 否  | 证书密钥的密码。默认值为空字符串。 |
1259
1260## PerformanceTiming<sup>11+</sup>
1261
1262性能打点(单位:毫秒)。
1263
1264**系统能力**:SystemCapability.Communication.NetStack
1265
1266| 名称       | 类型   | 必填   | 说明                   |
1267| ---------- | ------ | ---- | --------------------- |
1268| dnsTiming  | number | 是   | 从[request](#request)请求到DNS解析完成耗时。 |
1269| tcpTiming  | number | 是   | 从[request](#request)请求到TCP连接完成耗时。 |
1270| tlsTiming  | number | 是   | 从[request](#request)请求到TLS连接完成耗时。 |
1271| firstSendTiming  | number | 是   | 从[request](#request)请求到开始发送第一个字节的耗时。 |
1272| firstReceiveTiming  | number | 是   | 从[request](#request)请求到接收第一个字节的耗时。 |
1273| totalFinishTiming  | number | 是   | 从[request](#request)请求到完成请求的耗时。 |
1274| redirectTiming  | number | 是   | 从[request](#request)请求到完成所有重定向步骤的耗时。 |
1275| responseHeaderTiming  | number | 是   | 从[request](#request)请求到header解析完成的耗时。 |
1276| responseBodyTiming  | number | 是   | 从[request](#request)请求到body解析完成的耗时。 |
1277| totalTiming  | number | 是   | 从[request](#request)请求回调到应用程序的耗时。 |
1278
1279## DataReceiveProgressInfo<sup>11+</sup>
1280
1281数据接收信息。
1282
1283**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。
1284
1285**系统能力**:SystemCapability.Communication.NetStack
1286
1287| 名称 | 类型 | 必填 | 说明                |
1288| ---- | ---- | ---- |-------------------|
1289|  receiveSize        | number | 是  | 已接收的数据量(单位:字节)。   |
1290| totalSize| number | 是 | 总共要接收的数据量(单位:字节)。 |
1291
1292## DataSendProgressInfo<sup>11+</sup>
1293
1294数据发送信息。
1295
1296**原子化服务API:** 从API version 15开始,该接口支持在原子化服务中使用。
1297
1298**系统能力**:SystemCapability.Communication.NetStack
1299
1300### 属性
1301
1302| 名称 | 类型 | 必填 | 说明 |
1303| ---- | ---- | ---- | ---- |
1304| sendSize        | number | 是  | 每次发送的数据量(单位:字节)。  |
1305| totalSize | number | 是 | 总共要发送的数据量(单位:字节)。 |
1306
1307## MultiFormData<sup>11+</sup>
1308
1309多部分表单数据的类型。
1310
1311**系统能力**:SystemCapability.Communication.NetStack
1312
1313| 名称 | 类型 | 必填 | 说明 |
1314| ---- | ---- | ---- | ---- |
1315| name        | string | 是  | 数据名称。                                                                      |
1316| contentType | string | 是 | 数据类型,如'text/plain','image/png', 'image/jpeg', 'audio/mpeg', 'video/mp4'等。 |
1317| remoteFileName | string | 否 | 上传到服务器保存为文件的名称。<br>**说明**:指定该字段后,请求头中会添加filename字段,表示上传到服务器文件的名称。<br>(1)当上传数据为文件时,若通过data字段指定文件内容,通常需要设置remoteFileName字段,用以指定上传到服务器文件的名称(实际结果与服务器具体行为有关);若通过filePath字段指定文件路径,请求头中会自动添加filename字段,其默认值为filePath中的文件名称,如需特殊指定,也可通过本字段对filename重新设置。<br>(2)当上传数据为二进制格式时,则必须设置remoteFileName字段。                                                 |
1318| data | string \| Object \| ArrayBuffer | 否 | 表单数据内容。                                                 |
1319| filePath | string | 否 | 此参数根据文件的内容设置mime部件的正文内容。用于代替data将文件数据设置为数据内容,如果data为空,则必须设置filePath。如果data有值,则filePath不会生效。|
1320
1321## http.createHttpResponseCache<sup>9+</sup>
1322
1323createHttpResponseCache(cacheSize?: number): HttpResponseCache
1324
1325创建一个HttpResponseCache对象,可用于存储HTTP请求的响应数据。对象中可调用[flush](#flush9)与[delete](#delete9)方法,cacheSize指定缓存大小。
1326
1327**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
1328
1329**系统能力**:SystemCapability.Communication.NetStack
1330
1331**参数:**
1332
1333| 参数名   | 类型                                    | 必填 | 说明       |
1334| -------- | --------------------------------------- | ---- | ---------- |
1335| cacheSize | number | 否 | 缓存大小。最大为10\*1024\*1024(10MB),默认最大。 |
1336
1337**返回值:**
1338
1339| 类型        | 说明                                                         |
1340| :---------- | :----------------------------------------------------------- |
1341| [HttpResponseCache](#httpresponsecache9) | 返回一个存储HTTP访问请求响应的对象。 |
1342
1343**示例:**
1344
1345```ts
1346import { http } from '@kit.NetworkKit';
1347
1348let httpResponseCache = http.createHttpResponseCache();
1349```
1350
1351## HttpResponseCache<sup>9+</sup>
1352
1353存储HTTP访问请求响应的对象。在调用HttpResponseCache的方法前,需要先通过[createHttpResponseCache()](#httpcreatehttpresponsecache9)创建一个任务。
1354
1355**响应头中的相应关键字使用**
1356
1357- **`Cache-Control`**:用于指定缓存策略,如`no-cache`, `no-store`, `max-age`, `public`, `private`等。
1358
1359- **`Expires`**:指定资源的过期时间,格式为GMT时间。
1360
1361- **`ETag`**:用于资源版本标识,客户端可以使用`If-None-Match`请求头来验证资源是否已更改。
1362
1363- **`Last-Modified`**:指定资源最后修改时间,客户端可以使用`If-Modified-Since`请求头来验证资源是否已更改。
1364
1365- **`Vary`**:指定哪些请求头的值会影响缓存的响应,用于区分不同的缓存版本。
1366
1367使用这些关键字时,服务器端需要正确配置响应头,客户端则需要根据这些响应头来决定是否使用缓存的资源,以及如何验证资源是否是最新的。正确的缓存策略可以显著提高应用的性能和用户体验。
1368
1369**如何设置Cache-Control头**
1370
1371`Cache-Control`为通用报头,但通常是在服务器端进行的,允许定义一个响应资源应该何时、如何被缓存以及缓存多长时间。以下是一些常用的`Cache-Control`指令及其含义:
1372
1373- **`no-cache`**:表示在使用缓存前,必须先去源服务器校验资源的有效性。如果资源未变更,则响应状态码为304(Not Modified),不发送资源内容,使用缓存中的资源。如果资源已经过期,则响应状态码为200(OK),并发送资源内容。
1374
1375- **`no-store`**:表示不允许缓存资源,每次请求都必须从服务器获取资源。
1376
1377- **`max-age`**:指定缓存的最大时间(以秒为单位)。例如,`Cache-Control: max-age=3600`表示缓存的有效期为1小时。
1378
1379- **`public`**:表明响应可以被任何对象(包括:发送请求的客户端,代理服务器等)缓存。
1380
1381- **`private`**:表明响应只能被单个用户缓存,不能作为共享缓存(即代理服务器不能缓存)。
1382
1383- **`must-revalidate`**:表示必须在使用缓存前验证旧资源的状态,并且在缓存过期后,需要重新验证资源。
1384
1385- **`no-transform`**:表示不允许代理服务器修改响应内容。
1386
1387- **`proxy-revalidate`**:与`must-revalidate`类似,但仅适用于共享缓存。
1388
1389- **`s-maxage`**:类似于`max-age`,但仅适用于共享缓存。
1390
1391### flush<sup>9+</sup>
1392
1393flush(callback: AsyncCallback\<void\>): void
1394
1395将缓存中的数据写入文件系统,以便在下一个HTTP请求中访问所有缓存数据,使用callback方式作为异步方法。缓存数据包括:响应头(header)、响应体(result)、cookies、请求时间(requestTime)和响应时间(responseTime)。
1396
1397**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
1398
1399**系统能力**:SystemCapability.Communication.NetStack
1400
1401**参数:**
1402
1403| 参数名   | 类型                                    | 必填 | 说明       |
1404| -------- | --------------------------------------- | ---- | ---------- |
1405| callback | AsyncCallback\<void\> | 是   | 回调函数。返回写入结果。当写入成功时,err为undefined,否则为错误对象。 |
1406
1407**示例:**
1408
1409```ts
1410import { http } from '@kit.NetworkKit';
1411import { BusinessError } from '@kit.BasicServicesKit';
1412
1413let httpResponseCache = http.createHttpResponseCache();
1414let httpRequest = http.createHttp();
1415httpRequest.request("EXAMPLE_URL", (err: BusinessError, data: http.HttpResponse) => {
1416  if (!err) {
1417    httpResponseCache.flush((err: BusinessError) => {
1418      if (err) {
1419        console.error('flush fail');
1420      }
1421      console.info('flush success');
1422    });
1423    httpRequest.destroy();
1424  } else {
1425    console.error('error:' + JSON.stringify(err));
1426    // 当该请求使用完毕时,开发者务必调用destroy方法主动销毁该JavaScript Object。
1427    httpRequest.destroy();
1428  }
1429});
1430```
1431
1432### flush<sup>9+</sup>
1433
1434flush(): Promise\<void\>
1435
1436将缓存中的数据写入文件系统,以便在下一个HTTP请求中访问所有缓存数据,使用Promise方式作为异步方法。
1437
1438**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
1439
1440**系统能力**:SystemCapability.Communication.NetStack
1441
1442**返回值:**
1443
1444| 类型                              | 说明                                  |
1445| --------------------------------- | ------------------------------------- |
1446| Promise\<void\> | Promise对象。无返回结果的Promise对象。 |
1447
1448**示例:**
1449
1450```ts
1451import { http } from '@kit.NetworkKit';
1452import { BusinessError } from '@kit.BasicServicesKit';
1453
1454let httpRequest = http.createHttp();
1455let httpResponseCache = http.createHttpResponseCache();
1456let promise = httpRequest.request("EXAMPLE_URL");
1457
1458promise.then((data: http.HttpResponse) => {
1459  httpResponseCache.flush().then(() => {
1460    console.error('flush success');
1461  }).catch((err: BusinessError) => {
1462    console.error('flush fail');
1463  });
1464}).catch((err: Error) => {
1465  console.error('error:' + JSON.stringify(err));
1466});
1467```
1468
1469### delete<sup>9+</sup>
1470
1471delete(callback: AsyncCallback\<void\>): void
1472
1473禁用缓存并删除其中的数据,使用callback方式作为异步方法。
1474
1475**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
1476
1477**系统能力**:SystemCapability.Communication.NetStack
1478
1479**参数:**
1480
1481| 参数名   | 类型                                    | 必填 | 说明       |
1482| -------- | --------------------------------------- | ---- | ---------- |
1483| callback | AsyncCallback\<void\> | 是   | 回调函数。当删除成功时,err为undefined,否则为错误对象。|
1484
1485**示例:**
1486
1487```ts
1488import { http } from '@kit.NetworkKit';
1489import { BusinessError } from '@kit.BasicServicesKit';
1490
1491let httpRequest = http.createHttp();
1492httpRequest.request("EXAMPLE_URL").then(data => {
1493  const httpResponseCache = http.createHttpResponseCache();
1494  httpResponseCache.delete((err: BusinessError) => {
1495    try {
1496      if (err) {
1497        console.error('fail: ' + err);
1498      } else {
1499        console.info('success');
1500      }
1501    } catch (err) {
1502      console.error('error: ' + err);
1503    }
1504  });
1505  httpRequest.destroy();
1506}).catch((error: BusinessError) => {
1507  console.error("errocode" + JSON.stringify(error));
1508});
1509```
1510
1511### delete<sup>9+</sup>
1512
1513delete(): Promise\<void\>
1514
1515禁用缓存并删除其中的数据,使用Promise方式作为异步方法。
1516
1517**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
1518
1519**系统能力**:SystemCapability.Communication.NetStack
1520
1521**返回值:**
1522
1523| 类型                              | 说明                                  |
1524| --------------------------------- | ------------------------------------- |
1525| Promise\<void\> | Promise对象。无返回结果的Promise对象。 |
1526
1527**示例:**
1528
1529```ts
1530import { http } from '@kit.NetworkKit';
1531import { BusinessError } from '@kit.BasicServicesKit';
1532
1533let httpRequest = http.createHttp();
1534httpRequest.request("EXAMPLE_URL").then(data => {
1535  const httpResponseCache = http.createHttpResponseCache();
1536  httpResponseCache.delete().then(() => {
1537    console.log("success");
1538  }).catch((err: BusinessError) => {
1539    console.error("fail");
1540  });
1541  httpRequest.destroy();
1542}).catch((error: BusinessError) => {
1543  console.error("errocode" + JSON.stringify(error));
1544});
1545```
1546
1547## HttpDataType<sup>9+</sup>
1548
1549HTTP的数据类型。
1550
1551**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
1552
1553**系统能力**:SystemCapability.Communication.NetStack
1554
1555| 名称 | 值 | 说明     |
1556| ------------------  | -- | ----------- |
1557| STRING              | 0 | 字符串类型。 |
1558| OBJECT              | 1 | 对象类型。    |
1559| ARRAY_BUFFER        | 2 | 二进制数组类型。|
1560
1561## HttpProtocol<sup>9+</sup>
1562
1563HTTP协议版本。
1564
1565**系统能力**:SystemCapability.Communication.NetStack
1566
1567| 名称  |   值   | 说明                                                                   |
1568| :-------- | :----------- |:---------------------------------------------------------------------|
1569| HTTP1_1   |   0   | 协议HTTP1.1。 <br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。       |
1570| HTTP2     |   1   | 协议HTTP2。<br>**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。          |
1571| HTTP3<sup>11+</sup> |  2  | 协议HTTP3,若系统或服务器不支持,则使用低版本的HTTP协议请求。<br />**注意:** 仅对HTTPS的URL生效,HTTP则会请求失败。 |
1572
1573## CertType<sup>11+</sup>
1574
1575枚举,证书类型。
1576
1577**系统能力**:SystemCapability.Communication.NetStack
1578
1579| 名称 |   值   | 说明       |
1580| --- | ------ | ---------- |
1581| PEM | PEM | 证书类型PEM。 |
1582| DER | DER | 证书类型DER。 |
1583| P12 | P12 | 证书类型P12。 |
1584
1585## CertificatePinning<sup>12+</sup>
1586
1587由应用配置的证书。
1588
1589**系统能力**:SystemCapability.Communication.NetStack
1590
1591|  名称  |  类型  |  必填  |说明     |
1592| ------------------  |---- |-- | ----------- |
1593| publicKeyHash       | string | 是 |字符串类型的证书PIN码。 |
1594| hashAlgorithm        | 'SHA-256' |  是  |加密算法,当前仅支持该算法。 |
1595
1596## HttpProxy<sup>10+</sup>
1597
1598type HttpProxy = connection.HttpProxy
1599
1600网络代理配置信息。
1601
1602**原子化服务API:** 从API version 11开始,该接口支持在原子化服务中使用。
1603
1604**系统能力**:SystemCapability.Communication.NetStack
1605
1606|       类型       |            说明             |
1607| ---------------- | --------------------------- |
1608| connection.HttpProxy | 网络代理配置信息。     |
1609
1610## AddressFamily<sup>15+</sup>
1611
1612枚举,解析目标域名时限定的地址类型。
1613
1614**系统能力**:SystemCapability.Communication.NetStack
1615
1616|       名称       |     值     |            说明             |
1617| ---------------- | --------------- | --------------------------- |
1618| DEFAULT | CURL_IPRESOLVE_WHATEVER | 设置此选项后,系统将自行选择目标域名的IPv4地址或IPv6地址。     |
1619| ONLY_V4 | CURL_IPRESOLVE_V4 | 设置此选项后,系统仅解析目标域名的IPv4地址,忽略IPv6地址。     |
1620| ONLY_V6 | CURL_IPRESOLVE_V6 | 设置此选项后,系统仅解析目标域名的IPv6地址,忽略IPv4地址。     |
1621
1622## Credential<sup>18+</sup>
1623
1624会话中服务器身份验证设置所使用的身份验证凭据,包括用户名和密码。
1625
1626**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。
1627
1628**系统能力**:SystemCapability.Communication.NetStack
1629
1630|  名称  |  类型  |  只读  | 可选  |说明     |
1631| ------------------  |---- |-- | -- |----------- |
1632| username       | string | 否 |否 |用于身份验证的用户名。默认值为' '。 |
1633| password        | string |  否  |否 |用于身份验证的密码。默认值为' '。 |
1634
1635## ServerAuthentication<sup>18+</sup>
1636
1637HTTP服务器身份验证。
1638
1639**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。
1640
1641**系统能力**:SystemCapability.Communication.NetStack
1642
1643|  名称               |  类型                                            | 只读    |    可选     |说明     |
1644| ------------------  |-------------------------------------------------|-------- |------------ |---------------|
1645| credential          | [Credential](#credential18)                     | 否      | 否         |服务器的凭证。默认值为undefined。     |
1646| authenticationType  | [AuthenticationType](#authenticationtype18)     | 否      | 是        | 服务器的认证类型。如果没有设置,需与服务器协商。     |
1647
1648
1649## TlsConfig<sup>18+</sup>
1650
1651TLS加密版本及套件配置。
1652
1653**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。
1654
1655**系统能力**:SystemCapability.Communication.NetStack
1656
1657|  名称               |  类型                            | 只读    |    可选     |说明     |
1658| ------------------  |---------------------------------|-------- |-------- |---------------|
1659| tlsVersionMin       | [TlsVersion](#tlsversion18)     | 否      |否       | TLS最低版本号。     |
1660| tlsVersionMax        | [TlsVersion](#tlsversion18)    | 否      |否       | TLS最高版本号。     |
1661| cipherSuites        | [CipherSuite](#ciphersuite18)[] | 否      |是       | 声明加密套件类型的数组。 |
1662
1663## TlsVersion<sup>18+</sup>
1664
1665枚举,TLS版本号。
1666
1667**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。
1668
1669**系统能力**:SystemCapability.Communication.NetStack
1670
1671| 名称        | 值 | 说明         |
1672|:----------|:--|:-----------|
1673| TLS_V_1_0 | 4 | TLS版本号1.0。 |
1674| TLS_V_1_1 | 5 | TLS版本号1.1。 |
1675| TLS_V_1_2 | 6 | TLS版本号1.2。 |
1676| TLS_V_1_3 | 7 | TLS版本号1.3。 |
1677
1678## TlsOptions<sup>18+</sup>
1679
1680type TlsOptions = 'system' | TlsConfig
1681
1682TLS配置。
1683
1684**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。
1685
1686**系统能力**:SystemCapability.Communication.NetStack
1687
1688| 类型                         | 说明                                                                                 |
1689|-------------------------------|------------------------------------------------------------------------------------|
1690| 'system'  | 表示使用系统的TLS版本,是未进行TLS设置的默认值,值固定为'system'字符串。 |
1691| TlsConfig | 表示使用自定义的TLS版本号和加密套件。 |
1692
1693## RemoteValidation<sup>18+</sup>
1694
1695type RemoteValidation = 'system' | 'skip'
1696
1697验证远程服务器身份的方式。
1698
1699**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。
1700
1701**系统能力**:SystemCapability.Communication.NetStack
1702
1703| 类型                         | 说明                                                                                 |
1704|-------------------------------|------------------------------------------------------------------------------------|
1705| 'system'  | 表示使用系统CA验证远端服务器身份,值固定为'system'字符串,是未配置时的默认值。 |
1706| 'skip'   | 表示跳过验证远端服务器身份流程,值固定为'skip'字符串。 |
1707
1708## AuthenticationType<sup>18+</sup>
1709
1710type AuthenticationType = 'basic' | 'ntlm' | 'digest'
1711
1712在会话中的服务器身份验证时可以设置使用不同的身份验证机制。
1713
1714**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。
1715
1716**系统能力**:SystemCapability.Communication.NetStack
1717
1718|类型                          | 说明                                                                                                 |
1719|-------------------------------|----------------------------------------------------------------------------------------------------|
1720| 'basic'  | 表示使用基本认证方式,值固定为'basic'字符串。 |
1721| 'ntlm'   | 表示使用ntlm认证方式,值固定为'ntlm'字符串。 |
1722| 'digest' | 表示使用摘要认证方式,值固定为'digest'字符串。 |
1723
1724## CipherSuite<sup>18+</sup>
1725
1726type CipherSuite = TlsV13CipherSuite
1727
1728加密套件声明函数。
1729
1730**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。
1731
1732**系统能力**:SystemCapability.Communication.NetStack
1733
1734|       类型       | 说明                                                                |
1735| ---------------- |-------------------------------------------------------------------|
1736| TlsV13CipherSuite | 表示值的类型为[TlsV13CipherSuite](#tlsv13ciphersuite18)。                 |
1737
1738## TlsV13CipherSuite<sup>18+</sup>
1739
1740type TlsV13CipherSuite = TlsV12CipherSuite | TlsV13SpecificCipherSuite
1741
1742TLS1.3的加密套件声明函数,支持TLS1.3版本,兼容TLS1.2版本。
1743
1744**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。
1745
1746**系统能力**:SystemCapability.Communication.NetStack
1747
1748|       类型       | 说明                                                                |
1749| ---------------- |-------------------------------------------------------------------|
1750| TlsV12CipherSuite | 表示值的类型为[TlsV11CipherSuite](#tlsv11ciphersuite18)。                 |
1751| TlsV13SpecificCipherSuite | 表示值的类型为[TlsV13SpecificCipherSuite](#tlsv13specificciphersuite18)。 |
1752
1753## TlsV12CipherSuite<sup>18+</sup>
1754
1755type TlsV12CipherSuite = TlsV11CipherSuite | TlsV12SpecificCipherSuite
1756
1757TLS1.2的加密套件声明函数,支持TLS1.2版本,兼容TLS1.1版本。
1758
1759**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。
1760
1761**系统能力**:SystemCapability.Communication.NetStack
1762
1763|       类型       | 说明                                                                |
1764| ---------------- |-------------------------------------------------------------------|
1765| TlsV11CipherSuite | 表示值的类型为[TlsV11CipherSuite](#tlsv11ciphersuite18)。                 |
1766| TlsV12SpecificCipherSuite | 表示值的类型为[TlsV12SpecificCipherSuite](#tlsv12specificciphersuite18)。 |
1767
1768## TlsV11CipherSuite<sup>18+</sup>
1769
1770type TlsV11CipherSuite = TlsV10CipherSuite
1771
1772TLS1.1的加密套件声明函数,与TLS1.0的加密套件相同。
1773
1774**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。
1775
1776**系统能力**:SystemCapability.Communication.NetStack
1777
1778|       类型       | 说明                                                |
1779| ---------------- |---------------------------------------------------|
1780| TlsV10CipherSuite | 表示值的类型为[TlsV10CipherSuite](#tlsv10ciphersuite18)。 |
1781
1782## TlsV10CipherSuite<sup>18+</sup>
1783
1784type TlsV10CipherSuite = TlsV10SpecificCipherSuite
1785
1786TLS1.0的加密套件声明函数。
1787
1788**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。
1789
1790**系统能力**:SystemCapability.Communication.NetStack
1791
1792|       类型       | 说明                                                                |
1793| ---------------- |-------------------------------------------------------------------|
1794| TlsV10SpecificCipherSuite | 表示值的类型为[TlsV10SpecificCipherSuite](#tlsv10specificciphersuite18)。 |
1795
1796## TlsV13SpecificCipherSuite<sup>18+</sup>
1797
1798type TlsV13SpecificCipherSuite = 'TLS_AES_128_GCM_SHA256' | 'TLS_AES_256_GCM_SHA384' | 'TLS_CHACHA20_POLY1305_SHA256'
1799
1800TLS1.3及以上版本支持的加密套件。
1801
1802**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。
1803
1804**系统能力**:SystemCapability.Communication.NetStack
1805
1806|       类型       | 说明   |
1807| ---------------- |------|
1808| 'TLS_AES_128_GCM_SHA256' | 表示值的类型为字符串,可取'TLS_AES_128_GCM_SHA256'。 |
1809| 'TLS_AES_256_GCM_SHA384' | 表示值的类型为字符串,可取'TLS_AES_256_GCM_SHA384'。 |
1810| 'TLS_CHACHA20_POLY1305_SHA256' | 表示值的类型为字符串,可取'TLS_CHACHA20_POLY1305_SHA256'。 |
1811
1812## TlsV12SpecificCipherSuite<sup>18+</sup>
1813
1814type TlsV12SpecificCipherSuite = 'TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256' | 'TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256' |
1815'TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384' | 'TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384' |
1816'TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256' | 'TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256' |
1817'TLS_RSA_WITH_AES_128_GCM_SHA256' | 'TLS_RSA_WITH_AES_256_GCM_SHA384'
1818
1819TLS1.2及以上版本支持的加密套件。
1820
1821**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。
1822
1823**系统能力**:SystemCapability.Communication.NetStack
1824
1825|       类型       | 说明   |
1826| ---------------- |------|
1827| 'TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256' | 表示值的类型为字符串,可取'TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256'。 |
1828| 'TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256' | 表示值的类型为字符串,可取'TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256'。 |
1829| 'TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384' | 表示值的类型为字符串,可取'TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384'。 |
1830| 'TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384' | 表示值的类型为字符串,可取'TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384'。 |
1831| 'TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256' | 表示值的类型为字符串,可取'TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256'。 |
1832| 'TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256' | 表示值的类型为字符串,可取'TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256'。 |
1833| 'TLS_RSA_WITH_AES_128_GCM_SHA256' | 表示值的类型为字符串,可取'TLS_RSA_WITH_AES_128_GCM_SHA256'。 |
1834| 'TLS_RSA_WITH_AES_256_GCM_SHA384' | 表示值的类型为字符串,可取'TLS_RSA_WITH_AES_256_GCM_SHA384'。 |
1835
1836## TlsV10SpecificCipherSuite<sup>18+</sup>
1837
1838type TlsV10SpecificCipherSuite = 'TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA' | 'TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA' |
1839'TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA' | 'TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA' | 'TLS_RSA_WITH_AES_128_CBC_SHA' |
1840'TLS_RSA_WITH_AES_256_CBC_SHA' | 'TLS_RSA_WITH_3DES_EDE_CBC_SHA'
1841
1842TLS1.0及以上版本支持的加密套件。
1843
1844**原子化服务API:** 从API version 18开始,该接口支持在原子化服务中使用。
1845
1846**系统能力**:SystemCapability.Communication.NetStack
1847
1848|       类型       | 说明   |
1849| ---------------- |------|
1850| 'TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA' | 表示值的类型为字符串,可取'TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA'。 |
1851| 'TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA' | 表示值的类型为字符串,可取'TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA'。 |
1852| 'TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA' | 表示值的类型为字符串,可取'TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA'。 |
1853| 'TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA' | 表示值的类型为字符串,可取'TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA'。 |
1854| 'TLS_RSA_WITH_AES_128_CBC_SHA' | 表示值的类型为字符串,可取'TLS_RSA_WITH_AES_128_CBC_SHA'。 |
1855| 'TLS_RSA_WITH_AES_256_CBC_SHA' | 表示值的类型为字符串,可取'TLS_RSA_WITH_AES_256_CBC_SHA'。 |
1856| 'TLS_RSA_WITH_3DES_EDE_CBC_SHA' | 表示值的类型为字符串,可取'TLS_RSA_WITH_3DES_EDE_CBC_SHA'。 |
1857
1858## SslType<sup>20+</sup>
1859
1860type SslType = 'TLS' | 'TLCP'
1861
1862安全通信协议。
1863
1864**原子化服务API:** 从API version 20开始,该接口支持在原子化服务中使用。
1865
1866**系统能力**:SystemCapability.Communication.NetStack
1867
1868| 类型   | 说明                                   |
1869| ------ | -------------------------------------- |
1870| 'TLS' | 表示使用TLS安全通信协议,值固定为'TLS'字符串。   |
1871| 'TLCP' | 表示使用TLCP安全通信协议,值固定为'TLCP'字符串。 |
1872