我是第一次使用WebClient
的Spring网络流量模块。
其想法是查询某个URL,这给了我一个shift-jis
编码的CSV。
String content = webClient
.method(HttpMethod.GET)
.acceptCharset(Charset.forName("shift-jis"))
.uri(uri)
.accept(MediaType.TEXT_PLAIN, new MediaType("text", "csv"))
.retrieve()
.toEntity(String.class)
.mapNotNull(HttpEntity::getBody)
.block();
这有点工作,但content
字符串看起来像是编码错误--可能是UTF-8。
或者,Apache HttpClient
版本似乎有效:
HttpClient client = HttpClientBuilder.create().build();
HttpGet get = new HttpGet(uri);
String content = IOUtils.toString(
client.execute(get).getEntity().getContent(),
Charset.forName("shift-jis"));
所以我想知道如何才能干扰在toEntity
中发生的转换。我尝试将它添加到webClient
调用中:
.codecs(configurer -> {
StringDecoder decoder = StringDecoder.textPlainOnly();
decoder.setDefaultCharset(Charset.forName("shift-jis"));
configurer.customCodecs().registerWithDefaultConfig(decoder);
})
唉,这似乎还没有被人接受。但是,在StringDecoder#decode
中设置断点并在调试期间手动覆盖defaultCharset
字段确实有帮助。
发布于 2022-03-05 16:15:19
啊..。刚发完帖子,我就意识到:也许我的自定义text/csv
mime类型并不真正被解释为textPlainOnly
。
而且,实际上:
StringDecoder decoder = StringDecoder.allMimeTypes();
很明显这招很管用。
如果我的方法非常麻烦,并且有更好/正确的方法来解决这个问题,那么这个问题就没什么可提的了。
https://stackoverflow.com/questions/71363816
复制相似问题