jQuery in Action, 3rd edition
The moose likes Servlets and the fly likes Unable to read the Japanese character from HTTP Response Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Unable to read the Japanese character from HTTP Response" Watch "Unable to read the Japanese character from HTTP Response" New topic

Unable to read the Japanese character from HTTP Response

saravanan kanda swamy
Ranch Hand

Joined: Apr 21, 2004
Posts: 33

I have a problem like this from my application Server (Weblogic 6.1) I am sending a request to another web application and based on that request it will send a response with the japanese characters.

While reading the response from that I am able to get all the data clearly but only with the Japanese data, it is coming as junk data. I am using HttpUrlConnection to send the request.

Here is the piece of code that I have used.

/** Post a string to an URL and get the reply as a string. Returns an empty
string if things didn't work out. */
static public String getURLPostString(URL url, String body) {
StringBuffer sb = new StringBuffer();

// find the newline character(s) on the current system
String newline = null;
try {
newline = System.getProperty("line.separator");
} catch (Exception e) {
newline = "\n";

try {
// URL must use the http protocol!
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setAllowUserInteraction(false); // you may not ask the user
conn.setDoOutput(true); // we want to send things
// the Content-type should be default, but we set it anyway
conn.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" );
// the content-length should not be necessary, but we're cautious
conn.setRequestProperty( "Content-length", Integer.toString(body.length()));

// get the output stream to POST our form data
OutputStream rawOutStream = conn.getOutputStream();
PrintWriter pw = new PrintWriter(rawOutStream);

pw.print(body); // here we "send" our body!

// get the input stream for reading the reply
// IMPORTANT! Your body will not get transmitted if you get the
// InputStream before completely writing out your output first!
InputStream rawInStream = conn.getInputStream();

// get response
BufferedReader rdr = new BufferedReader(new InputStreamReader(rawInStream));
String line;

while ((line = rdr.readLine()) != null) {
return sb.toString();
} catch (Exception e) {
System.out.println("Exception "+e.toString());
return ""; // an exception occurred

And also the other web application is able to print the japanese data in the console and it is perfectly fine. The problem that I am facing is while reading the reponse. I am using InputStream and when I try to print that (japanese data) it is coming as junk data.

That charset I am using is SHIFT_JIS, and In my Servlet I am setting the Character encoding as SHIFT_JIS.

Please help me out what is cause for that problem

Thanks in advance
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

Did you try passing "Shift_JIS" to the InputStreamReader ?

[My Blog]
All roads lead to JavaRanch
Paul Clapham

Joined: Oct 14, 2005
Posts: 19738

saravanan kanda swamy
Ranch Hand

Joined: Apr 21, 2004
Posts: 33

Thanks. I set the SHIFT_JIS in the InputStreamReader. That solved the problem.

Thanks a Lot

saravanan kanda swamy
Ranch Hand

Joined: Apr 21, 2004
Posts: 33

I have a new problem now. In another web application I am sending the japanese characters in the text box when I try to print that it is coming in corrupted format, but I have set
contentType="text/html; charset=SHIFT_JIS" pageEncoding="Shift_JIS"

in the page attribute of the JSP

and in the Servlet also I have set the reqeust in SHIFT_JIS encoding. the problem now is when I try to print the the japanese data it is coming in different values.

To debug I printed the reponse.getCharacterEncoding() it is coming as ISO-8859-1 cant we change that to SHIFT_JIS?

How to change the reponse encoding to SHIFT_JIS?

I am using weblogic 6.1

Thanks in advance.

I agree. Here's the link: http://aspose.com/file-tools
subject: Unable to read the Japanese character from HTTP Response
It's not a secret anymore!