如何从头到String获取最后修改日期

时间:2011-10-21 07:14:21

标签: java http http-headers httpclient

我在Header Object中获得了Header值。但是我需要将“Last-Modified”放入字符串对象中进行比较。请问您能告诉我如何将最后一个标题放入字符串中。

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);

3 个答案:

答案 0 :(得分:1)

在许多情况下,您只获得一个Last-Modified标头,因此您只需使用:

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

对于多个值,JavaDoc说:如果存在具有给定名称的响应头并包含多个值,则将返回首先添加的值。

答案 1 :(得分:0)

尝试这样的事情:

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

答案 2 :(得分:0)

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

    return "";
}