1

In the code below:

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 is constructed with UTF-8 because of setRequestProperty of HttpsURLConnection. However, I think the code really needs to get the CharSet from the response. But it seems like a chicken-and-the-egg problem.

Is it possible to retrieve the CharSet the server actually used in its response (instead of setting it to what I {wished|hoped} for)?

jww
  • 97,681
  • 90
  • 411
  • 885

1 Answers1

0

Typically, the best you can do is set the Accept-Charset request header to indicate the character encodings your client accepts. For example,

Accept-Charset: UTF-8

If the HTTP server is properly configured, the response will be UTF-8. The response Content-Type should contain the response's character encoding.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Thanks @Sotirios. I think that's what I am doing with `setRequestProperty`. And a misconfigured server is what I am concerned with. Its why I'd like to know what the server actually responded with so I can create the proper `InputStreamReader`. – jww Jul 08 '14 at 15:03
  • @jww You're using `setRequestProperty` but setting the wrong header. Check the response headers and the body to verify the content-type. – Sotirios Delimanolis Jul 08 '14 at 16:22