【问题标题】:How to get a Confluence page through REST in UTF-8 in Java?如何在 Java 中通过 UTF-8 中的 REST 获取 Confluence 页面?
【发布时间】:2021-06-25 06:40:49
【问题描述】:

我正在尝试通过他们的 API REST 获取 Confluence 页面。我需要那个页面有 UTF-8 编码,因为我在我的页面上使用了特殊字符。

我现在正在使用这个调用:

URL url = new URL(https_url);

InetSocketAddress inet = new InetSocketAddress(proxy, port);
Proxy proxy = new Proxy(Proxy.Type.HTTP, inet);

HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(proxy);
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json;charset=UTF-8");
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.setRequestProperty("Authorization", auth);

// JSON
StringBuilder finalJson = new StringBuilder();
InputStreamReader input = new InputStreamReader(conn.getInputStream());
BufferedReader buffered = new BufferedReader(input );

String json;
while ((json = buffered .readLine()) != null) {
    finalJson.append(json);
}

但是有了这个,我的 json 是用 ASCII 写的。

我该如何解决这个问题?

【问题讨论】:

  • 这是云还是服务器?
  • 这是云,我的朋友。
  • 要修复的一件事:确保您的 InputStreamReader 通过指定字符编码将数据解析为 UTF-8:InputStreamReader input = new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8); 否则它将使用您系统的默认编码,这可能会或可能会不是 UTF-8。
  • 非常感谢!这个输入配置解决了我的问题! ♥
  • 仅仅请求 UTF-8 并不能保证响应真的是 UTF-8。

标签: java http java-8 utf-8 confluence


【解决方案1】:

InputStreamReader还有一个构造函数,它有两个参数,第一个是InputStream,第二个是字符集名称,也许你可以试试。

InputStreamReader input = new InputStreamReader(conn.getInputStream(), "utf8");

【讨论】:

  • 正如@Jesper 首先说的高一点,这就是解决方案。谢谢!
猜你喜欢
  • 2015-06-03
  • 2017-05-09
  • 2016-12-25
  • 2020-12-22
  • 1970-01-01
  • 2021-02-27
  • 1970-01-01
  • 1970-01-01
  • 2017-03-10
相关资源
最近更新 更多