Files
youle_app_ohos/platform/src/main/ets/net/HttpClient.ets
T
lanterngamescn ecb0e4642a M2(平台层): KvStore/FileSystem/HttpClient/Downloader/Unzipper/PermissionGuard/LocalUploadServer
T-M2-01 存储/文件:KvStore(preferences 持久化 urlpath/upurlpath)、FileSystem(fileIo 建删读写/列目录/拷贝内置 rawfile)
T-M2-02 网络/下载:HttpClient(GET 禁缓存,契约 §4.3)、Downloader(http 流式写盘+进度,原生异步不阻塞 UI)
T-M2-03 解压:Unzipper(zlib.decompressFile,原生异步)。偏差:下载/解压用原生异步 API 即满足"UI 不阻塞",
        独立 TaskScheduler(TaskPool) 推迟到 M5 真有 CPU 密集任务(MD5)时再加,避免投机性死代码。
T-M2-04 权限/上传:PermissionGuard(用时申请,沙箱文件免存储权限)、LocalUploadServer(骨架,本机端口服务 M3 落地)

devecocli build 通过。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-25 18:14:41 +08:00

40 lines
1.4 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 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<HttpResult> {
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();
}
}
}