import java.io.*;
import java.net.*;
/**
* 獲取一個(gè)網(wǎng)頁的源代碼
* @author Tony
*/
public class GetCode {
//方法:獲取網(wǎng)頁的源代碼
public static String getNetcode(String spec)
{
String line = null;
String temp = null;
try{
URL url=new URL(spec); //設(shè)置URL
HttpURLConnection uc = (HttpURLConnection)url.openConnection();//打開連接
//獲取到輸入流
BufferedReader in=new BufferedReader(new InputStreamReader(uc.getInputStream()));
while((line=in.readLine())!=null)
{
temp+=line;//向temp里添加網(wǎng)頁代碼
}
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; //實(shí)際發(fā)送的漫畫名稱關(guān)鍵字
String url = null;//發(fā)送去的url地址
try
{
sendName = URLEncoder.encode(bookName,"gb2312");
//實(shí)際發(fā)送的漫畫類型
String sendCategory = URLEncoder.encode(category,"gb2312");
//整合url 并帶上查詢關(guān)鍵子、類型
url = new String("
//打印出得到的網(wǎng)頁代碼
System.out.println(getNetcode(url));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
代碼如上。這個(gè)代碼對于baidu首頁的源代碼能夠正常獲取到,但對baidu顯示查詢結(jié)果頁面卻不能獲取到,報(bào)錯(cuò)為:Unexpected end of file from server 請問為什么呢?有什么解決方法嗎?
posted on 2007-05-25 14:40
細(xì)雨游風(fēng) 閱讀(722)
評論(0) 編輯 收藏