获取网页的源码内容,我知道的有两种方法,已经比较两种方法得到的源码是完全一致的,只是消耗时间相差很多,下面是代码:
public static void main(String[] args) throws ClientProtocolException, IOException {
Long start = new Date().getTime();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet=new HttpGet("http://www.cnblogs.com/yakov/archive/2011/11/25/httpclient_crawl_pdf.html");
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity=httpResponse.getEntity();
InputStream inStream=httpEntity.getContent();
String content="";
InputStreamReader reader=new InputStreamReader(inStream);
BufferedReader brs=new BufferedReader(reader);
while((content=brs.readLine())!=null){
System.out.println(content);
}
brs.close();
reader.close();
inStream.close();
System.out.println("耗时:"+(new Date().getTime()-start));
Long end = new Date().getTime();
URL url=new URL("http://www.cnblogs.com/yakov/archive/2011/11/25/httpclient_crawl_pdf.html");
InputStreamReader isr=new InputStreamReader(url.openStream());
BufferedReader br=new BufferedReader(isr);
String str;
while((str=br.readLine())!=null){
System.out.println(str);
}
br.close();
isr.close();
System.out.println("耗时:"+(new Date().getTime()-end));
}
每个机器运行的时间肯定是不相同,但是肯定能测试出来哪个性能更好。
本人电脑上第一个耗时为:580
第二个耗时为:260