/** * HTTP 客户端(框架 §8.2 / 附录 A.4)。封装 @kit.NetworkKit http。 * * 用于远程配置请求:GET、**强制禁缓存**(usingCache:false,对齐契约 §4.3 "禁缓存 + ?a=时间戳")。 * 明文 HTTP 域名须在 module.json5 网络安全配置放行(附录 B)。 */ import { http } from '@kit.NetworkKit'; import { Logger } from 'common'; /** HTTP 文本响应结果。 */ export interface HttpResult { /** HTTP 状态码(如 200)。 */ code: number; /** 响应体文本(非字符串时为空串)。 */ body: string; } export class HttpClient { private static readonly log: Logger = Logger.tag('HttpClient'); /** GET 文本,禁缓存。失败抛出(由调用方降级到本地缓存)。 */ static async getString(url: string, timeoutMs: number = 15000): Promise { const req: http.HttpRequest = http.createHttp(); try { const resp: http.HttpResponse = await req.request(url, { method: http.RequestMethod.GET, expectDataType: http.HttpDataType.STRING, usingCache: false, connectTimeout: timeoutMs, readTimeout: timeoutMs, }); const body: string = typeof resp.result === 'string' ? resp.result : ''; HttpClient.log.i(`GET ${url} -> ${resp.responseCode} (${body.length} chars)`); return { code: resp.responseCode, body }; } finally { req.destroy(); } } }