aspose file tools*
The moose likes Servlets and the fly likes HTTP HEAD method: how to get the metadata returned Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "HTTP HEAD method: how to get the metadata returned" Watch "HTTP HEAD method: how to get the metadata returned" New topic
Author

HTTP HEAD method: how to get the metadata returned

Russell Bateman
Ranch Hand

Joined: Feb 26, 2008
Posts: 69
Not certain I picked the right JavaRanch forum here, but the Apache-Tomcat one seemed pretty Tomcat specific.

My question is really about getting the metadata back from a server promising information via the HEAD method. I have Googled hard, but find no sample code nor enough information other than to know that HeadMethod.getReponseBody() isn't supposed to return anything. And, in the case of the server I'm trying to talk to, it doesn't.

My code is slightly modified GET code (which, as GET code, works fine). I do get returned back from executeMethod() with a 200, so there's no error there. (This is a copy, paste and slightly adjust job here, so I hope I didn't leave anything out.) I just don't know how to get what HEAD is supposed to give me.

Thanks for any help,

Russ Bateman

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41906
    
  63
The point of a HEAD request is to get the response headers. If you inspect the array returned by the method.getResponseHeaders() call you'll find various useful information in it (like the size of the body if this were a GET, and the date when it was last modified).


Ping & DNS - my free Android networking tools app
Russell Bateman
Ranch Hand

Joined: Feb 26, 2008
Posts: 69
Thank you very much for responding, I had read something about



However, I don't have the HttpConnection as you see from my sample code, and I have not figured out how yet to get it.

I'm also in the dark as to what to do with an instance of HttpState after creating it.

This is why I was looking for some sample code. I readily admit I'm a newbie.

Thanks for any other help you can give.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41906
    
  63
You can get both HttpState and HttpConnection through methods in the HttpClient class (for the connection there's an intermediate step of getting the HttpConnectionManager first). But reading the response body will do no good for HEAD, as you've found out, because the body is empty.
Russell Bateman
Ranch Hand

Joined: Feb 26, 2008
Posts: 69
My bad: I had used search to find HttpMethod.getResponseHeaders() and stumbled frustratingly again upon HeadMethod.readResponseBody() all over again and didn't recompare the name with what you had written. I stumbled blindly onward. Grrr.

So, to HttpMethod.getResponseHeaders(), there are no arguments I need to pass. That is the answer I was looking for. Thank you for your patience.

Russ Bateman
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: HTTP HEAD method: how to get the metadata returned