1

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

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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

    return "";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.