隨筆-10  評論-10  文章-0  trackbacks-0
            2007年5月25日


          import java.io.*;
          import java.net.*;


          /**
          * 獲取一個網頁的源代碼
          * @author Tony
          */
          public class GetCode {

          //方法:獲取網頁的源代碼 
          public static String getNetcode(String spec)
          {
           String line = null;
           String temp = null;
           try{
             URL url=new URL(spec); //設置URL
            
             HttpURLConnection uc = (HttpURLConnection)url.openConnection();//打開連接
             //獲取到輸入流
             BufferedReader in=new BufferedReader(new InputStreamReader(uc.getInputStream()));
           
             while((line=in.readLine())!=null)
             {
              temp+=line;//向temp里添加網頁代碼
             }
             if(in!=null)
             in.close();
            }
            catch(Exception e)
            {
             e.printStackTrace();
            }
            
            return temp;
          }
          /**
          * @param args
          */
          public static void main(String[] args) {
           
           String bookName = "傻B巴西的卡卡";//最初要搜尋的漫畫名稱
           String category = "漫畫"; //類型
           String sendName; //實際發送的漫畫名稱關鍵字
           String url = null;//發送去的url地址
           
            try
            {
             sendName = URLEncoder.encode(bookName,"gb2312");
             //實際發送的漫畫類型
             String sendCategory = URLEncoder.encode(category,"gb2312");
             //整合url 并帶上查詢關鍵子、類型
             url = new String("

             //打印出得到的網頁代碼
             System.out.println(getNetcode(url));
            }
            catch(Exception e)
            {
             e.printStackTrace();
            }

           

           }
          }



          代碼如上。這個代碼對于baidu首頁的源代碼能夠正常獲取到,但對baidu顯示查詢結果頁面卻不能獲取到,報錯為:Unexpected end of file from server   請問為什么呢?有什么解決方法嗎?
          posted @ 2007-05-25 14:40 細雨游風 閱讀(723) | 評論 (0)編輯 收藏
          主站蜘蛛池模板: 太和县| 于都县| 贞丰县| 天门市| 大连市| 胶南市| 横峰县| 宁安市| 霸州市| 行唐县| 加查县| 怀集县| 泰安市| 利川市| 吴江市| 贵州省| 湖北省| 油尖旺区| 雷波县| 晋中市| 舞阳县| 共和县| 绥化市| 峨眉山市| 昔阳县| 福州市| 湛江市| 千阳县| 河东区| 内丘县| 扶余县| 兰西县| 山东省| 利辛县| 全南县| 阜南县| 庆安县| 图木舒克市| 桦南县| 甘肃省| 六枝特区|