encoding/decoding of Strings HTTP/IBM850/ISO8859_1

Hi,

I do a HTTP request in java, the web server send me back html

data encoded in IBM850:

<META content="text/html; charset=IBM850" http-equiv="Content-Type">

For my java program, I need to have String encoded in ISO8859_1, so

I try to do a conversion which does not work.

Any one has an idea to help me ?

Here is the code:

// HTTP request

URL server =new URL("http://www....");

HttpURLConnection connection = (HttpURLConnection) server.openConnection();

connection.setDoInput(true);

connection.connect();

InputStream in = connection.getInputStream();

BufferedReader bin =new BufferedReader(new InputStreamReader( in));

// Read data from the server

StringBuffer sb =new StringBuffer();

String line = bin.readLine();

while (line !=null){

sb.append(line);

sb.append("\r\n");

line = bin.readLine();

}

String resultat = sb.toString();

// Conversion

if ((resultat.indexOf("charset=IBM850")!=-1) || (resultat.indexOf("charset=ibm850")!=-1)){

// conversion to iso latin

try{

resultat =new String(resultat.getBytes("ISO8859_1"),"IBM850");

}catch(java.io.UnsupportedEncodingException uee){

System.out.println("Unsupported conversion");

}

}

Note : the page sent back by the HTTP server is really encoded in IBM850

Internet Explorer or Firefox correctly display the corresponding html page

[2462 byte] By [reseau-emploia] at [2007-10-1 8:36:18]
# 1
I suggest you use InputStreamReader with explicit encoding of "ISO-8859-1" or you end up decoding as if the page was in whatever is your platform's default file.encoding and then trying to get bytes as if it was ISO-8859-1 - that may cause your problems. Rest of your code seems to be OK
hlavaca at 2007-7-9 22:42:07 > top of Java-index,Desktop,I18N...
# 2
If your webserver sends correct encoding in the Content-Type header, you could save you all this and just use correct encoding with the reader ;-)
hlavaca at 2007-7-9 22:42:07 > top of Java-index,Desktop,I18N...
# 3
Thanks for the answers...Awarded duke dollars
reseau-emploia at 2007-7-9 22:42:07 > top of Java-index,Desktop,I18N...