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 細雨游風 閱讀(722) | 評論 (0) | 編輯 收藏 新手求救老程序員 其他新手也可來看看 請問下JAVA哥哥,我想寫個程序:輸入查詢關鍵字,然后調用baidu or google的數據庫進行查詢,然后返回前10條結果給我。 有什么辦法嗎? 先在此謝謝您~! posted @ 2007-05-21 14:26 細雨游風 閱讀(280) | 評論 (0) | 編輯 收藏
//打印出得到的網頁代碼 System.out.println(getNetcode(url)); } catch(Exception e) { e.printStackTrace(); }
} }