How can I set the encoding of a httpExchange response?

ckersch

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?

saka1029

Use Content-Type header to specify encoding.

String encoding = "UTF-8";

httpExchange.getResponseHeaders().set("Content-Type", "text/html; charset=" + encoding);

Writer out = new OutputStreamWriter(httpExchange.getResponseBody(), encoding));
out.write(something);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I set encoding to ANSI in PhpStorm?

From Dev

How can I set encoding to ANSI in PhpStorm?

From Java

How can I set response header on express.js assets

From Dev

How can I set the name for a file that is to be downloaded in a Koa response?

From Dev

How can I set data's in twig from Ajax response

From Dev

How do I set an encoding type on NSInputStream?

From Dev

How can I set VIM's default encoding to UTF-8?

From Dev

How can I fix my character encoding?

From Dev

How can I use AIFF encoding with abcde?

From Dev

Express.JS: how can I set response status by name rather than number?

From Dev

How can I set an HTML5 canvas ImageData from an XMLHttpRequest response?

From Dev

Jasmine how do I set the response of a spy

From Dev

How to set the encoding in a TableView

From Dev

How to set encoding for bundle?

From Java

How can I mock requests and the response?

From Dev

How can I force send response stream?

From Dev

How can I wrap a JSON response in Spring

From Dev

how can I parse ajax response in jquery?

From Dev

How can I download json Response with Scrapy?

From Dev

How can I use the response from a JOptionPane?

From Dev

How can I reduce the size of a response to an HttpWebRequest

From Dev

How can I wrap a JSON response in Spring

From Dev

How can I use the response from a JOptionPane?

From Dev

How can I return the Http response?

From Dev

Flask, How can I response the picture on the page

From Dev

How can I set these permissions?

From Dev

What is this encoding and how can I encode a string to it in PHP?

From Dev

How can I exclude a plugin from grails default gsp encoding?

From Dev

How can I programmatically change file encoding linux?

Related Related

HotTag

Archive