yxhxj2006

          常用鏈接

          統(tǒng)計

          最新評論

          Java通過URL獲取網(wǎng)站Html源代碼

          package com.wsw.j2se.url;

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

          /**
           * 通過網(wǎng)站域名URL獲取該網(wǎng)站的源碼
           * 
          @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);
              }

              
              
          /**
               * 通過網(wǎng)站域名URL獲取該網(wǎng)站的源碼
               * 
          @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二進制數(shù)據(jù)
                  byte[] data = readInputStream(inStream);        //把二進制數(shù)據(jù)轉(zhuǎn)化為byte字節(jié)數(shù)據(jù)
                  String htmlSource = new String(data);
                  
          return htmlSource;
              }

              
              
          /**
               * 把二進制流轉(zhuǎn)化為byte字節(jié)數(shù)組
               * 
          @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 奮斗成就男人 閱讀(4526) 評論(3)  編輯  收藏

          評論

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

          http://www.xe.com/currencytables/?from=USD
          從上面的網(wǎng)址獲取代碼,返回狀態(tài)307,但是沒有代碼..求助   回復(fù)  更多評論   

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

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

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

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


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 建始县| 唐河县| 邹平县| 安义县| 海淀区| 四会市| 长宁县| 临海市| 东乡族自治县| 达日县| 高邮市| 温州市| 巢湖市| 蕲春县| 兴山县| 鹤壁市| 永清县| 乌兰察布市| 荔浦县| 龙门县| 洮南市| 黔西| 枝江市| 五莲县| 友谊县| 静海县| 雅江县| 遂川县| 都兰县| 吉林市| 文山县| 交口县| 达州市| 大同县| 本溪市| 福鼎市| 菏泽市| 盐源县| 陆良县| 当雄县| 西畴县|