隨筆-10  評論-10  文章-0  trackbacks-0


          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   請問為什么呢?有什么解決方法嗎?

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


          網站導航:
           
          主站蜘蛛池模板: 崇文区| 鄂托克旗| 靖安县| 桐乡市| 韶关市| 诸城市| 确山县| 青铜峡市| 汉沽区| 灌南县| 鄂尔多斯市| 东乡族自治县| 调兵山市| 吴忠市| 乐山市| 沁源县| 区。| 北海市| 体育| 罗定市| 岳西县| 隆回县| 离岛区| 平阳县| 阳朔县| 澄城县| 高尔夫| 磐安县| 项城市| 天等县| 班戈县| 澄城县| 凉城县| 吴桥县| 高碑店市| 如东县| 庆安县| 潜山县| 佳木斯市| 交城县| 襄汾县|