java 獲取URL連結 內容

weixin_33816946發表於2016-12-27

public static String getHtmlConentByUrl(String ssourl) {
        try {    
         URL url = new URL(ssourl);
         HttpURLConnection con = (HttpURLConnection) url.openConnection();
            
         con.setInstanceFollowRedirects(false);
         con.setUseCaches(false);
         con.setAllowUserInteraction(false);
         con.connect();             
         StringBuffer sb = new StringBuffer();
         String line = "";
         BufferedReader URLinput = new BufferedReader(new InputStreamReader(con.getInputStream()));
         while ((line = URLinput.readLine()) != null) {
          sb.append(line);
         }
         con.disconnect();
       
         return sb.toString().toLowerCase();
         
        } catch (Exception e) {
          
         return null;
        }

}

相關文章