如何从标头获取上次修改日期到字符串

发布于 2024-12-11 04:22:09 字数 681 浏览 0 评论 0原文

我已经在标头对象中获得了标头值。但我需要将“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);

I have got the Header values in Header Object. but I need "Last-Modified" into the string object for comparison. 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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧故 2024-12-18 04:22:09

在许多情况下,您只获得一个 Last-Modified 标头,因此您可以简单地使用:

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

对于多个值,JavaDoc 表示:如果具有给定名称的响应标头存在并且包含多个值,则使用第一个添加的值将被退回。

In many circumstances, you get just one Last-Modified header, so you could simply use:

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.

土豪我们做朋友吧 2024-12-18 04:22:09

尝试这样的事情:

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

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
阳光下慵懒的猫 2024-12-18 04:22:09
private String getLastModifiedDate(HttpResponse response) {
    Header header = response.getFirstHeader("Date");
    if (header != null) {
        return header.getValue();
    }

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

    return "";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文