Java InputStreamReader的HttpsURLConnection和字符集?

Java InputStreamReader的HttpsURLConnection和字符集?,java,character-encoding,httpurlconnection,inputstreamreader,Java,Character Encoding,Httpurlconnection,Inputstreamreader,在下面的代码中: HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); connection.setRequestProperty("charset", "utf-8"); InputStream input = connection.getInputStream(); InputStreamReader reader = new InputStreamReader(input, "utf-8");

在下面的代码中:

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);
...
由于
HttpsURLConnection
setRequestProperty
,使用
UTF-8
构造
InputStreamReader
。但是,我认为代码确实需要从响应中获取
字符集。但这似乎是鸡和蛋的问题


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

通常,您最好设置
接受字符集
请求头,以指示客户端接受的字符编码。比如说,

Accept-Charset: UTF-8

如果HTTP服务器配置正确,响应将为UTF-8。响应
内容类型
应包含响应的字符编码。

谢谢@Sotirios。我想这就是我使用
setRequestProperty
所做的。我关心的是配置错误的服务器。这就是为什么我想知道服务器实际响应了什么,这样我就可以创建正确的
InputStreamReader
@jww,您正在使用
setRequestProperty
,但是设置了错误的头。检查响应标题和正文以验证内容类型。