日期:2014-05-20  浏览次数:20749 次

怎样获取网页的最后更新时间????
如题
我的程序怎么老是输出0呢?

import   java.net.*;
public   class   Test   {

/**
  *   @param   args
  */
public   static   void   main(String[]   args)   {
//   TODO   Auto-generated   method   stub
try   {
URL   u   =   new   URL( "http://163.com/ ");
HttpURLConnection   con   =   (HttpURLConnection)u.openConnection();
con.connect();
long   a   =   con.getLastModified();
System.out.println(a);
}   catch   (Exception   e){
System.out.println( "connection   error! ");
}

}

}


------解决方案--------------------
import java.net.*;
import java.util.*;
class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {


URL u = new URL( "http://community.csdn.net/Expert/topic/5277/5277719.xml ");
HttpURLConnection con = (HttpURLConnection)u.openConnection();
con.connect();
long a = con.getLastModified();
System.out.println(new Date(con.getDate()));
System.out.println(a);
} catch (Exception e){
System.out.println( "connection error! ");
}

}}
------解决方案--------------------
http协议标准的http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html打不开,不清楚这个header是否是MUST

163的,我用flashget看过了,

它的response header:
Mon Jan 08 12:46:12 2007 HTTP/1.1 200 OK
Mon Jan 08 12:46:12 2007 Via: 1.0 ISA20060704
Mon Jan 08 12:46:12 2007 Connection: close
Mon Jan 08 12:46:12 2007 Proxy-Connection: close
Mon Jan 08 12:46:12 2007 Age: 96
Mon Jan 08 12:46:12 2007 Expires: Mon, 08 Jan 2007 04:49:53 GMT
Mon Jan 08 12:46:12 2007 Date: Mon, 08 Jan 2007 04:44:53 GMT
Mon Jan 08 12:46:12 2007 Content-Type: text/html; charset=GB2312
Mon Jan 08 12:46:12 2007 Server: Apache/2.0.54 (Unix)
Mon Jan 08 12:46:12 2007 Accept-Ranges: bytes
Mon Jan 08 12:46:12 2007 Cache-Control: max-age=300
Mon Jan 08 12:46:12 2007 Vary: Accept-Encoding
Mon Jan 08 12:46:12 2007 X-Pad: avoid browser bug
Mon Jan 08 12:46:12 2007 X-Cache: HIT from news.163.com

没有这个header,而上面那个rfc2616的header里面有:
Mon Jan 08 12:43:11 2007 HTTP/1.1 200 OK
Mon Jan 08 12:43:11 2007 Via: 1.1 ISA20060704
Mon Jan 08 12:43:11 2007 Connection: close
Mon Jan 08 12:43:11 2007 Proxy-Connection: close
Mon Jan 08 12:43:11 2007 Content-Length: 126444
Mon Jan 08 12:43:11 2007 Expires: Mon, 08 Jan 2007 10:43:17 GMT
Mon Jan 08 12:43:11 2007 Date: Mon, 08 Jan 2007 04:43:17 GMT
Mon Jan 08 12:43:11 2007 Content-Type: text/html; charset=iso-8859-1
Mon Jan 08 12:43:11 2007 ETag: "4135cda4 "
Mon Jan 08 12:43:11 2007 Server: Apache/1.3.37 (Unix) PHP/4.4.4
Mon Jan 08 12:43:11 2007 P3P: policyref= "http://www.w3.org/2001/05/P3P/p3p.xml "
Mon Jan 08 12:43:11 2007 Cache-Control: max-age=21600
Mon Jan 08 12:43:11 2007 Last-Modified: Wed, 01 Sep 2004 13:24:52 GMT
Mon Jan 08 12:43:11 2007 Accept-Ranges: bytes
Mon Jan 08 12:43:12 2007 Keep-Alive: timeout=2, max=100
------解决方案--------------------
貌似没有特别好的解决方案,楼主可以根据下面的优先度来决定最后更新时间

1 Last-modified
2 Date
3 客户端系统时间