如题
我的程序怎么老是输出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!");
}
}}
我的程序怎么老是输出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.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!");
}}}
or 0 if not known.
**************
根据http协议,应该有个last modified header field阿?
根据http协议,应该有个last modified header field阿?
========
先一下,在看spec.
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
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的含义更多些。对于静态文件,指系统保存的文件最后更新时间
2 Date
3 客户端系统时间
……
<script type="javascript/text">
<!-- hide script from old browsers
document.write("最后修改:" + document.lastModified)
// end hiding contents
-->
</script>
</BODY></HTML>