Problems encoding in utf8 with urlconnection response

B

Bronwyn Howard

Basically, I have set up a chat program using http request. One
person sends a message to the servlet, it gets stored in a
StringBuffer. Other end of chat polls using http requests, and if a
message is sitting there for them, it sends it back in a response
header.

It has to be multilingual, and my problem is that CJK type characters
just don't seem to be encoding. When message is orignally sent, it is
URLEncoder'ed. Servlet receives these characters, and they are fine
(at this point I write them to a log, which works). When the request
comes in for the message though, I figured I should just be able to
send the characters straight back, but it doesn't work. If I
URLEncoder them here, they become ???.

I have put a little method in there which escapes the foreign string
to unicode, so I can check what's up. In the servlet side the string
is fine just before I send it (eg \u30CF\u30A4) but when it gets to
the other side it's not fine (eg \00CF\00A4). Looks like problem with
encoding right? I've tried everything...that I can think of. I need
help.

In the servlet, it's stored in a StringBuffer, and I simply return it
in a header:
res.setContentType("text/html; charset=utf8");
res.setHeader( "responseMessage", responseMessage);
(where res = HttpServletResponse)

In the app:
connection = ( HttpsURLConnection ) url.openConnection();
responseMessage =
connection.getHeaderField("responseMessage");
(where connection = HttpUrlConnection and responseMessage = String)

Just suggestions would be good. It should just be a simple process,
but I'm worried I may end up having to override ContentHandlers, or
figuring out a method to escape the "\u" strings back to the foreign
characters.. eep.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top