Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use HEAD method of HTTPClient to get all headers

I have to use HEAD method of HttpClient to get the header field and to check the "last-modified" date of server file.
I am not able to get that, if you know how to get the header field then please reply. How to get the "last-modified" header into the String object for the comparison.

Here is my code:

HttpClient client = new DefaultHttpClient();
//HttpGet get = new HttpGet(url);
HttpHead method = new HttpHead(url);
HttpResponse response= client.execute(method);

Header[] s = response.getAllHeaders();

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());
}
like image 573
Shashi Shirke Avatar asked Jan 20 '26 11:01

Shashi Shirke


1 Answers

On httpClient 4.5 you would use:

final HttpHead headMethod = new HttpHead(fileUri);
final Header header = headMethod.getFirstHeader("last-modified");
final String lastModified = header.getValue();
like image 129
douglaslps Avatar answered Jan 23 '26 01:01

douglaslps



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!