随笔-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("http://www.baidu.com/s?ie=gb2312&wd="+sendName+"   "+sendCategory);

   //打印出得到的网页代码
   System.out.println(getNetcode(url));
  }
  catch(Exception e)
  {
   e.printStackTrace();
  }

 

 }
}



代码如上。这个代码对于baidu首页的源代码能够正常获取到,但对baidu显示查询结果页面却不能获取到,报错为:Unexpected end of file from server   请问为什么呢?有什么解决方法吗?
posted @ 2007-05-25 14:40 细雨游风 阅读(704) | 评论 (0)编辑 收藏