Request 是一个基于Httpclient4cj(OkHttp)的高效率 ArkTs HTTP 客户端
- 支持HTTP/2,允许所有同一个主机地址的请求共享同一个 socket 连接
- 连接池减少请求延时
- 缓存响应内容,避免一些完全重复的请求
- 简洁的链式调用API
- 支持多种responseBody转换策略
- 仓颉协程spawn支持
ohpm install @axzo/ok-request
开启网络请求权限 module.json5
"requestPermissions": [
{
"name": "ohos.permission.INTERNET"
}
],
创建配置config
const config: OkConfig = {
requestInterceptors: [],
responseInterceptors: [],
timeout: 30,
maxConnections: 5,
baseUrl: undefined,
protocols: undefined,
tlsConfig: {
verifyMode: VerifyMode.ALL,
pem: undefined
}
}
config.requestInterceptors.push({
intercept: (request: Request) => {
return request.newBuilder()
.head('1', '2')
.build()
}
})
config.responseInterceptors.push({
intercept: (response: Response | undefined) => {
return response
}
})
创建client
let client = new OkHttpClient(config)
let res: ResponseBody = await this.client.get('https://baidu.com').send()
json body
let res: ResponseBody = await this.client.post('xx/xx/xx').json({
s: '1',
a: 2
}).send()
let res: ResponseBody = await this.client.post('xx/xx/xx').json({
s: '1',
a: 2
}).head('name', 'value').head('xxx', 'xxxx').send()
TEXT 解析
res?.text()
JSON 解析
res?.json<T>()
ArrayBuffer
res?.bytes()
取消当前client所有请求
this.client.cancelAll()
根据requestId取消请求
let request = this.client.get('https://baidu.com').build()
await this.client.execute(request)
this.client.cancel(request)
let result = await this.client.post('xxx.xxx.xxx').file(new FileBody(path, contentType)).send()
let multipart = new MultiPartBodyBuilder()
.addFormDataPart('file', 'filename.txt', new FileBody(path, "text/plain"))
.addTextFormDataPart('key', 'dsa')
.build()
let result = await this.client.post('xxx.xxx.xxx').multipart(multipart).send()
try {
await this.client.get('https://baidu.com').send()
} catch(e: HttpError) {
}