我使用以下代码来获取页面内容:
URL url=new URL("http://www.google.com.hk/intl/zh-CN/privacy.html");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openConnection().getInputStream()));
for(String line=reader.readLine();line!=null;line=reader.readLine()){
System.out.println(line);
}
reader.close();
页面:http ://www.google.com.hk/intl/zh-CN/privacy.html字符集是“UTF-8”,但我的系统默认字符集是“GBK”,所以,这些代码不能正确输入.
我知道,我可以在 InputStreamReader 构造函数中编写一个字符集名称:
new InputStreamReader(url.openConnection().getInputStream(),"UTF-8")
没关系,但我想知道:
如何检测字符集并获取页面内容?(最好不要发送两个请求)
任何java库都可以做到这一点?(获取网页内容,不需要设置字符集)
感谢帮助 :)