1

在下面的代码中:

HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestProperty("charset", "utf-8");

InputStream input = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(input, "utf-8");
BufferedReader buffer = new BufferedReader(reader);
...

An是由InputStreamReader构造UTF-8setRequestPropertyHttpsURLConnection但是,我认为代码确实需要CharSet从响应中获取。但这似乎是一个先有鸡还是先有蛋的问题。

是否可以检索CharSet在其响应中实际使用的服务器(而不是将其设置为我 {wished|hoped} 的)?

4

1 回答 1

0

通常,您可以做的最好的事情是设置Accept-Charset请求标头以指示您的客户端接受的字符编码。例如,

Accept-Charset: UTF-8

如果 HTTP 服务器配置正确,响应将是 UTF-8。响应Content-Type应包含响应的字符编码。

于 2014-07-08T05:59:32.840 回答