Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the encoding of a httpExchange response?

Tags:

People also ask

How do you encode a response in Java?

The given content type may include a character encoding specification, for example, text/html;charset=UTF-8. The response's character encoding is only set from the given content type if this method is called before getWriter is called. This method may be called repeatedly to change content type and character encoding.

What is HttpExchange in Java?

public abstract class HttpExchange extends Object. This class encapsulates a HTTP request received and a response to be generated in one exchange. It provides methods for examining the request from the client, and for building and sending the response. A HttpExchange must be closed to free or reuse underlying resources ...


I'm trying to modify some server code which uses an httpExchangeobject to handle the server's response to the client.

My issue is that for responses containing characters not supported by iso-8859-1, such as Chinese characters, I get something like '????' in place of the characters. I'd like to set the encoding of the response to utf-8, but have thus far been unsuccessful in doing so.

I tried adding this line:

httpExchange.getResponseHeaders().put("charset", Arrays.asList("UTF-8"));

This successfully puts a "charset" header in the response, but I still can't send the characters I want in the response.

How do I set the encoding of the response to allow for these characters?