不说啥 直接上代码:

public class WebPage {
 
 HttpClient client;
 
 public void getContent(String strUrl) throws IOException
 {
  client = new HttpClient();
  
  GetMethod getMethod = new GetMethod("http://bbs.sjtu.edu.cn");
  int statusCode = client.executeMethod( getMethod );
  
  InputStream instream = getMethod.getResponseBodyAsStream();
  BufferedReader br = new BufferedReader( new InputStreamReader( instream, "utf-8"));
  
  String line;
  
  while( (line=br.readLine())!=null )
  {
   System.out.println( line );
  }
   
  getMethod.releaseConnection();
   }

}
 

注意,通过把输入流转为对应的编码方式“utf-8”。显示即正常。

但同样是这套代码,把URL换成百度主页,又显示乱码。这是为什么呢?

看看网页返回的内容:

charset=gb2312">�ٶ�һ�£����֪��