简体   繁体   English

如何从标题到字符串获取最后修改日期

[英]how to get last-modified date from header to String

I have got the Header values in Header Object. 我已经在Header Object中获得了Header值。 but I need "Last-Modified" into the string object for comparison. 但是我需要在字符串对象中添加“ Last-Modified”以进行比较。 Please could you tell me how should I get the last header into the string. 请您告诉我如何将最后一个标头插入字符串。

HttpClient client = new DefaultHttpClient();
//HttpGet get = new HttpGet(url);
HttpHead method = new HttpHead(url);
HttpResponse response = client.execute(method);
Header[] s = response.getAllHeaders();

String sh = String.valueOf(s);
System.out.println("The value of sh:"+sh);

System.out.println("The header from the httpclient:");

for (int i = 0; i < s.length; i++) {
  Header hd = s[i];
  System.out.println("Header Name: "+hd.getName() + "       " + " Header Value: " +  hd.getValue());
}

String last-modified =   // here I need to convert this header(last-modified);

In many circumstances, you get just one Last-Modified header, so you could simply use: 在许多情况下,您只会得到一个Last-Modified标头,因此您可以简单地使用:

String lastModified = response.getHeader("last-modified");
if (lastModified != null) {    // in case the header isn't set
  // do something
}

For multiple values, the JavaDoc says: If a response header with the given name exists and contains multiple values, the value that was added first will be returned. 对于多个值,JavaDoc说: 如果存在具有给定名称的响应头并且包含多个值,则将返回最先添加的值。

private String getLastModifiedDate(HttpResponse response) {
    Header header = response.getFirstHeader("Date");
    if (header != null) {
        return header.getValue();
    }

    return "";
}

Try something like this: 尝试这样的事情:

Header[] s = response.getHeaders("last-modified");
String lastModified = s[0].getValue();   // ! There might be more than 1 header
                                         // ! or none at all

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

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