如题
我的程序怎么老是输出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!");
}

}}

解决方案 »

  1.   

    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!");
    }}}
      

  2.   

    System.out.println(new Date(con.getDate()));这个打印出来应当不是0吧 。后面那个是打印出来0
      

  3.   

    the date the resource referenced by this URLConnection was last modified, 
    or 0 if not known.
       **************
      

  4.   

    我就是问为什么会是零阿??
    根据http协议,应该有个last modified header field阿?
      

  5.   

    我就是问为什么会是零阿??
    根据http协议,应该有个last modified header field阿?
    ========
    先一下,在看spec.
      

  6.   

    http协议标准的http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html打不开,不清楚这个header是否是MUST163的,我用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
      

  7.   

    估计在标准里面这个header属于SHOULD级别的
      

  8.   

    System.out.println(new Date(con.getDate()));
      

  9.   

    协议中,Last-modified果然是SHOULD级别的
    HTTP/1.1 servers SHOULD send Last-Modified whenever feasible.Date应该是永远取得到的,因为根据协议Origin servers MUST include a Date header field in all responses, 除非
    1 返回状态码100(继续)、101(切换协议)的时候,MAY include a Date header field
    2 服务器处理请求响应的时候,发生错误,比如返回状态码500(内部错误)、503(服务不可用)的时候
    3 服务器没有可靠的时钟(???)但是,这个和楼主需要的Last-modified属于两个概念。
    Date其实就是响应何时被输出的,而Last-modified的含义更多些。对于静态文件,指系统保存的文件最后更新时间
      

  10.   

    貌似没有特别好的解决方案,楼主可以根据下面的优先度来决定最后更新时间1 Last-modified
    2 Date
    3 客户端系统时间
      

  11.   

    在网面中最后加入以下Script:
    ……
    <script type="javascript/text">
    <!--  hide script from old browsers
        document.write("最后修改:" + document.lastModified)
        // end hiding contents 
    -->
    </script>
    </BODY></HTML>
      

  12.   

    js document.lastModified只能获取本页/或者本站其他网页的时间,对于跨域无能为力,而且lz似乎需要在服务器端运行