http请求的请求头为Object类型,本来就支持动态设置。
需要自定义类型来接收响应数据。
get('goods', (data: Object) => { let goods: Goods = JSON.parse(data.toString()); let type = goods.goods; console.info(type); // 依据服务器返回的商品类型再请求具体的商品列表。 get(type!, (data: Object) => { let sorts: Shoes = JSON.parse(data.toString()); this.shoes = sorts.shoes; let s: Clothing = JSON.parse(data.toString()); this.clothing = s.clothing; }); });
if (type === "goods") { // 客户端请求商品类型时随机返回 clothing 或 shoes res.json({ "goods": Math.random() > 0.5 ? "clothing" : "shoes" }) }
get(type!, (data: Object) => { let sorts: Shoes = JSON.parse(data.toString()); this.shoes = sorts.shoes; let s: Clothing = JSON.parse(data.toString()); this.clothing = s.clothing; });
完整示例参考如下:
客户端ArkTS代码:
import http from '@ohos.net.http'; import { BusinessError } from '@ohos.base'; interface Goods { goods: string; } interface Shoes { shoes: string[]; } interface Clothing { clothing: string[]; } let httpRequest = http.createHttp(); httpRequest.on('headersReceive', (header) => { console.info(`header: ${JSON.stringify(header)}`); }); function get(type: string, f: (data: Object) => void) { httpRequest.request( // 服务器url,运行服务器的电脑连手机wifi时可直接用ip代替域名。 'http://x.x.x.x:9588', { method: http.RequestMethod.GET, // header字段为Object,支持动态设置。 header: { 'type': type }, connectTimeout: 10000, // 可选,默认为60000ms. readTimeout: 10000, // 可选,默认为60000ms. }, (err: BusinessError, data: http.HttpResponse) => { if (!err) { f(data.result); } else { console.error(`error code:${err.code}, error message:${err.message}`); // 取消订阅http响应头事件。 httpRequest.off('headersReceive'); } } ); } @Entry @Component struct Index { private message: string = '获取商品'; @State shoes: string[] | undefined = []; @State clothing: string[] | undefined = []; build() { RelativeContainer() { Text(this.message) .id('get') .fontSize(50) .fontWeight(FontWeight.Bold) .alignRules({ center: { anchor: '__container__', align: VerticalAlign.Center }, middle: { anchor: '__container__', align: HorizontalAlign.Center } }) .onClick(() => { // 先发请求询问有哪些商品类型,服务器随机返回shoes或clothing. get('goods', (data: Object) => { let goods: Goods = JSON.parse(data.toString()); let type = goods.goods; console.info(type); // 依据服务器返回的商品类型再请求具体的商品列表。 get(type!, (data: Object) => { let sorts: Shoes = JSON.parse(data.toString()); this.shoes = sorts.shoes; let s: Clothing = JSON.parse(data.toString()); this.clothing = s.clothing; }); }); }); Text('获取的商品如下') .id('info') .alignRules({ top: { anchor: 'get', align: VerticalAlign.Bottom }, middle: { anchor: '__container__', align: HorizontalAlign.Center } }) .margin({ top: '5%' }); Row() { ForEach(this.shoes, (item: string) => { Text(item); }); ForEach(this.clothing, (item: string) => { Text(item); }); } .id('shoes') .alignRules({ top: { anchor: 'info', align: VerticalAlign.Bottom }, middle: { anchor: '__container__', align: HorizontalAlign.Center } }) .justifyContent(FlexAlign.SpaceEvenly) .width('100%') .backgroundColor(Color.Yellow) .margin({ top: '5%' }); } .height('100%') .width('100%'); } }
服务器端js代码:
const express = require('express'); const app = express(); /* GET home page. */ app.get('/', function (req, res, next) { let header = req.headers; console.log(JSON.stringify(header)) let type = header.type; if (type === "goods") { // 客户端请求商品类型时随机返回 clothing 或 shoes res.json({ "goods": Math.random() > 0.5 ? "clothing" : "shoes" }) } if (type === "clothing") { // 客户端请求clothing则返回clothing列表 res.json({ "clothing": ["coat", "shirt", "jacket"] }) } if (type === "shoes") { // 客户端请求shoes则返回shoes列表 res.json({ "shoes": ["slippers", "sneakers", "high heels"] }) } res.send('Home Page'); }); app.listen(9588, () => { console.info('服务器启动成功'); }); module.exports = app;
Q:http请求报2300023错误。
Q:http请求如何下载比较大的数据?
A:如果接收超过5M的数据,需要在HttpRequestOptions的maxLimit中进行设置。
Q:http请求头字段的默认值是什么?
A:当请求方式为"POST"、"PUT"、"DELETE"或者""时,默认{'content-Type': 'application/json'},否则默认{'content-Type': 'application/x-www-form-urlencoded'}。header等请求参数的类型与取值范围参见HttpRequestOptions。
Q:网络请求频繁2300028,如何排查?
A:TCP连接超时或读写超时,排查网络或服务器问题,例如网络是否稳定、信号强度是否较弱、服务器负载是否过高、处理速度是否正常等。
Q:网络请求是有数据长度限制吗?
A:expectDataType当指定其类型为Object时,最大长度为65536。
Q:HttpRequestOptions里面的method为GET时,extraData如果为Object,参数内容会拼接到url中进行发送。实际开发测试过程中,发现并没有拼接到url中。
A:Object的key-value作为string即可,如:extraData: {'start':'0','limit':'20'}。