0

I've got the following method to write HTTP response to browser socket.

public static void writeResponse(OutputStream os, HttpResponse response)
        throws IOException {    
    String total = just my http response text;  
    os.write(total.getBytes());
    //os.write(new String(total.getBytes(ISO_8859_1), UTF_8).getBytes()); - my try
    os.flush();
}

Charset is charset=UTF-8. Content type is text/plain.Text output text is

"Hello-eng\nПривет-rus\nこんにちは-jap\nनमस्ते-hind\nأهلا-arab"

But my browser can not read anything except English.

Hello-eng ������-rus ?????-jap ??????-hind ????-arab

What is wrong?

4
  • can you confirm you're using a modern standards compliant and up-to-date browser. Have you tried 2 different browsers. What happens if you show the page source or save it and view with a hex editor? Commented Jan 14, 2016 at 13:01
  • Chrome, Opera and Firefox has the same effect. If safe page and open in notepad, I will see English and Russian(!!!), but no others. Commented Jan 14, 2016 at 13:03
  • 1
    Try os.write(total.getBytes(UTF_8)); Commented Jan 14, 2016 at 13:20
  • @saka1029, it works. Post as an answer and will accept it. Commented Jan 14, 2016 at 14:05

2 Answers 2

2

Convert the response string to UTF_8 byte array. And write it to the response stream.

os.write(total.getBytes(UTF_8));
Sign up to request clarification or add additional context in comments.

Comments

1

You need to set the charset in the http header to match the encoding How can i change charset encoding in HTTP response in Java

1 Comment

But I set it. text/plain;charset=UTF-8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.