【发布时间】: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