tags:

views:

129

answers:

3

Hi all! How can i read http stream with HTML page in page's encoding? Here is a code fragment i use to get the http stream. InputStreamReader has the encoding optional argument, but i have no ideas about the way to obtain it.

URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
BufferedReader d = new BufferedReader(new InputStreamReader(is));
+3  A: 

Retrieving a Webpage is a reasonably complicated process. That's why libraries such as HttpClient exist. My advice is that unless you have a really compelling reason otherwise, use HttpClient.

cletus
+2  A: 

When the connection is establised thru

URLConnection conn = url.openConnection();

you can get the encoding method name thru url.getContentEncoding() so pass this String to InputStreamReader() so the code looks like

BufferedReader d = new BufferedReader(new InputStreamReader(is,url.getContentEncoding()));

Niger
there is no url.getContentEncoding() method :-(
Sure there is. http://java.sun.com/j2se/1.5.0/docs/api/java/net/URLConnection.html#getContentEncoding%28%29
Yishai
which version of java are you using pal?
Niger
sorry, you are right, i've tried class URL instead of URLConnection
A: 

The short answer is URLConnection.getContentEncoding(). The right answer is what cletus suggests, use an appropriate third party library unless you have a compelling reason not to.

Yishai
There is no self satisfaction unless the code is written in our won hand, rather seeking for third party.
Niger