Client Plugin
BOM 移除器
所需依赖项:io.ktor:ktor-client-bom-remover
代码示例: client-bom-remover
字节顺序标记 (BOM) 是编码在 Unicode 文件或流中的一个字符。BOM 的主要作用是指明文本流的编码方式以及 16 位和 32 位编码的字节序。
在某些情况下,有必要从响应体中移除 BOM。例如,在 UTF-8 编码中,BOM 的存在是可选的,当被无法处理 BOM 的软件读取时,可能会引发问题。
Ktor 客户端提供了 BOMRemover 插件,该插件可从 UTF-8、UTF-16 (BE)、UTF-16 (LE)、UTF-32 (BE) 和 UTF-32 (LE) 编码的响应体中移除 BOM。
请注意,在移除 BOM 时,Ktor 不会更改 Content-Length 标头,它仍会保留初始响应的长度。
添加依赖项
要使用 BOMRemover,你需要在构建脚本中包含 ktor-client-bom-remover 工件:
kotlin
implementation(ktorLibs.client.bomRemover) 上述代码段使用 Ktor 版本目录 来指定 Ktor 依赖项。
groovy
implementation ktorLibs.client.bomRemover 上述代码段使用 Ktor 版本目录 来指定 Ktor 依赖项。
xml
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-client-bom-remover-jvm</artifactId>
<version>${ktor_version}</version>
</dependency> 要了解更多信息,请参阅 Ktor 客户端所需的工件:添加客户端依赖项了解如何向现有项目添加客户端依赖项。。
安装 BOMRemover
要安装 BOMRemover,请将其传递给 客户端配置块 中的 install 函数:
kotlin
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.plugins.bomremover.*
//...
val client = HttpClient(CIO) {
install(BOMRemover)
}