yxhxj2006

          常用鏈接

          統計

          最新評論

          Java通過URL獲取網站Html源代碼

          package com.wsw.j2se.url;

          import java.io.ByteArrayOutputStream;
          import java.io.InputStream;
          import java.net.HttpURLConnection;
          import java.net.URL;

          /**
           * 通過網站域名URL獲取該網站的源碼
           * 
          @author Administrator
           *
           
          */

          public class HtmlRequest {
              
          /**
              * 
          @param args
              * 
          @throws MalformedURLException 
              
          */

              
          public static void main(String[] args) throws Exception    {
                  URL url 
          = new URL("http://www.ifeng.com"); 
                  String urlsource 
          = getURLSource(url);
                  System.out.println(urlsource);
              }

              
              
          /**
               * 通過網站域名URL獲取該網站的源碼
               * 
          @param url
               * 
          @return String
               * 
          @throws Exception
               
          */

              
          public static String getURLSource(URL url) throws Exception    {
                  HttpURLConnection conn 
          = (HttpURLConnection)url.openConnection();
                  conn.setRequestMethod(
          "GET");
                  conn.setConnectTimeout(
          5 * 1000);
                  InputStream inStream 
          =  conn.getInputStream();  //通過輸入流獲取html二進制數據
                  byte[] data = readInputStream(inStream);        //把二進制數據轉化為byte字節數據
                  String htmlSource = new String(data);
                  
          return htmlSource;
              }

              
              
          /**
               * 把二進制流轉化為byte字節數組
               * 
          @param instream
               * 
          @return byte[]
               * 
          @throws Exception
               
          */

              
          public static byte[] readInputStream(InputStream instream) throws Exception {
                  ByteArrayOutputStream outStream 
          = new ByteArrayOutputStream();
                  
          byte[]  buffer = new byte[1204];
                  
          int len = 0;
                  
          while ((len = instream.read(buffer)) != -1){
                      outStream.write(buffer,
          0,len);
                  }

                  instream.close();
                  
          return outStream.toByteArray();         
              }

          }

          posted on 2012-07-19 12:40 奮斗成就男人 閱讀(4525) 評論(3)  編輯  收藏

          評論

          # re: Java通過URL獲取網站Html源代碼 [未登錄] 2013-09-25 14:58 Robot

          http://www.xe.com/currencytables/?from=USD
          從上面的網址獲取代碼,返回狀態307,但是沒有代碼..求助   回復  更多評論   

          # re: Java通過URL獲取網站Html源代碼為什么代碼出來的不全 2013-12-06 13:10

          re: Java通過URL獲取網站Html源代碼為什么代碼出來的不全   回復  更多評論   

          # re: Java通過URL獲取網站Html源代碼 2015-02-05 23:29 zuidaima

          java通過java.net.HttpURLConnection類抓取網頁源碼工具類分享 http://zuidaima.com/share/2200487383485440.htm  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 永靖县| 秦安县| 南江县| 讷河市| 嘉定区| 嘉义市| 岳阳县| 女性| 会泽县| 东乌| 新巴尔虎右旗| 固始县| 仁寿县| 平安县| 长宁区| 常熟市| 宁城县| 庆安县| 方正县| 中宁县| 新田县| 新宁县| 嵊泗县| 黔东| 青铜峡市| 巴楚县| 丰城市| 千阳县| 湖北省| 辽阳市| 墨脱县| 高安市| 文山县| 阳高县| 那坡县| 久治县| 长治市| 扎鲁特旗| 大竹县| 乌鲁木齐县| 平度市|