Shashi Shirke
Shashi Shirke

Reputation: 93

How to implement the HEAD method of httpclient

Hi I am downloading file from server. I have to take meta-information using HEAD method. andybody help me to implement the HEAD method to get "last-modified" date and modified-since date.

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

//here I have to implement the HEAD method

Upvotes: 2

Views: 3330

Answers (1)

Abel
Abel

Reputation: 57149

The difference between a HEAD and a GET method is that the response will not contain a body. Otherwise, the two are the same. In other words, a HEAD method gets all the headers. It is not used for getting data of a single header, it just retrieves all headers at once.

In the code example you already have all headers, because you executed a HEAD request. In the for-loop you output all data from the headers. If the last-modified is not there, the server did not provide it for this resource.

Note that the if-modified-since is a request header field, not a response header field. You can set it to instruct the server to only return the resource if the modified-since date has passed. If you intend to only retrieve a resource when it has been modified on the server, you can just use a GET request with the if-modified-since header set. To know whether a server supports this header, check this tool: http://www.feedthebot.com/tools/if-modified/

Upvotes: 2

Related Questions