获取html代码如下:
public class MyBrowser {
public static void main(String[] args) {
try {
java.net.URL url = new java.net.URL("http://edu.daynews.com.cn/jyxw/list.html");
java.net.URLConnection conn = url.openConnection();
conn.connect();
System.out.println("" + conn.getContentType());
System.out.println("" + conn.getContentEncoding());//获取页面编码
System.out.println("" + conn.getContentLength());
System.out.println("" + conn.getDate());
System.out.println("" + conn.getExpiration());
System.out.println("" + conn.getLastModified());
StringBuffer contentBuffer = new StringBuffer();
java.io.InputStream is = conn.getInputStream();
//java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(is, conn.getContentEncoding()));
java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(is));
String inputLine = null;
while ((inputLine = reader.readLine()) != null) {
contentBuffer.append(inputLine);
contentBuffer.append("\n");
}
is.close();
System.out.println("" + contentBuffer.toString());
} catch (java.net.MalformedURLException e) {
e.printStackTrace();
} catch (java.io.IOException e) {
e.printStackTrace();
}
}
}
但是用以上方法获取的页面编码总是null,所以不能用获取到的页面编码来构建InputStreamReader对象,不明白原因在哪里?