URL读取页面源代码

   
   public String fetchhtml(String _url){
       
        String line,html=new String();
        URLConnection urlcon;
        BufferedReader br;
       
        try{
            URL url = new URL(_url);
            urlcon=(HttpURLConnection)url.openConnection();
            urlcon.setRequestProperty("User-agent","Mozilla/4.0");
            urlcon.connect();
            br = new BufferedReader(new                 InputStreamReader(urlcon.getInputStream(),
          Charset.forName("utf-8")));
            line = br.readLine();
            while(line!=null){
                 html+=line;
                 line = br.readLine();
            }
        }catch(Exception e){
            System.out.println("我错了");
        }
        return html;
   }

你可能感兴趣的:(职场,休闲)