Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to read the Japanese character from HTTP Response

 
saravanan kanda swamy
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.setRequestMethod("POST");
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!
pw.flush();
pw.close();

// 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) {
sb.append(line);
sb.append(newline);
}
return sb.toString();
} catch (Exception e) {
System.out.println("Exception "+e.toString());
e.printStackTrace();
}
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é
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try passing "Shift_JIS" to the InputStreamReader ?
 
Paul Clapham
Sheriff
Posts: 21318
32
Eclipse IDE Firefox Browser MySQL Database
 
saravanan kanda swamy
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

Thanks a Lot

Saravanan.K
 
saravanan kanda swamy
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

Saravanan.K
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic