response.asXml() 总是 returns Play Framework 编码错误

response.asXml() always returns encoding error on Play Framework

我正在制作 play framweork 应用程序。 我试图从网络服务中获取 xml 内容。 http://example.com/api returns xml,但它的编码是EUC-JP。 (charset=euc-jp)

我写了下面的代码。

WSRequest request = ws.url("http://example.com/api");
WSRequest complexRequest = request.setHeader("Accept", "application/xml")
    .setContentType("application/x-www-form-urlencoded");
Promise<Document> documentPromise = complexRequest.post("key1=value1").map(response -> {
    String name = XPath.selectText("//name", response.asXml());
    System.out.println("name :" + name);
    return response.asXml();
});

然而,response.asXml() 总是 returns 错误:

[Fatal Error] :xx:xx: Invalid byte 1 of 1-byte UTF-8 sequence.

如何使用 response.asXml 获取数据而不出现任何错误?

最后,我用DocumentBuilder代替了asXml,比如 How to fix Invalid byte 1 of 1-byte UTF-8 sequence

  Promise<Result> resultPromise = request.post("key=" + value).map(response -> {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    ByteArrayInputStream stream = new ByteArrayInputStream(response.getBody().getBytes("euc-jp"));
    String name = XPath.selectText("//name", builder.parse(stream)));
    System.out.println("name :" + name);

    return ok(main.render());
  });