This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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
Author

Unable to read the Japanese character from HTTP Response

saravanan kanda swamy
Ranch Hand

Joined: Apr 21, 2004
Posts: 33
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

Did you try passing "Shift_JIS" to the InputStreamReader ?


[My Blog]
All roads lead to JavaRanch
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

http://java.sun.com/developer/EJTechTips/2005/tt1220.html#2
saravanan kanda swamy
Ranch Hand

Joined: Apr 21, 2004
Posts: 33
Hi,

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

Thanks a Lot

Saravanan.K
saravanan kanda swamy
Ranch Hand

Joined: Apr 21, 2004
Posts: 33
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to read the Japanese character from HTTP Response
 
Similar Threads
HttpUrlConnection - cXML PunchOutSetupRequest
how to send a string to a servlet from a java program
java client communicating with servlet
Japanese character from the request stored in question mark in the DB
HttpURLConnection - Luanch Browser