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