简体   繁体   English

URL中的上次修改日期

[英]Last Modified date in URL

I'm writing client socket code in java and I found this piece of code which is supposed to read a line and check last modified date. 我在用Java编写客户端套接字代码,发现这段代码应该读取一行并检查上次修改日期。

I am confused why does it need to subtract the length by 21 in the modDateArr? 我很困惑,为什么需要在modDateArr中减去21的长度?

And is there any other way to do this? 还有其他方法吗?

while((x = br.readLine()) != null){
    if(x.contains("Last-Modified:")){
        modDateArr = new char[x.length()-21];
        x.getChars(20, x.length()-1, modDateArr, 0);
         // create mod date string from last mod info
        modDate = new String(modDateArr);
        break;
    }
}

Yes there is a better way: use URL and URLConnection : 是的,有更好的方法:使用URLURLConnection

URL url = new URL("http://blablah/foo");
URLConnection connection = url.openConnection();
Date lastModified = new Date(connection.getLastModified());

Note that the Last-Modified header looks like this: 请注意, Last-Modified标头如下所示:

Last-Modified: Wed, 15 Nov 1995 04:58:08 GMT

If you remove the first 21 characters from that line like your code does, you get this: 15 Nov 1995 04:58:08 GMT . 如果像代码一样从该行中删除前21个字符,则会得到以下结果: 15 Nov 1995 04:58:08 GMT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM