Java: get page headers and then close connection

I would like to get only the page titles for the url without loading the page content. This is what I am currently using:

...
URL targetUrl = new URL( urlValue );
HttpURLConnection conn = (HttpURLConnection) targetUrl.openConnection();
String value = conn.getHeaderField(0);
...

      

I'm not sure if this will cause the headers to stop or do anything.

+3


source to share


1 answer


Submit your HEAD

request instead GET

(which is the default method).

conn.setRequestMethod("HEAD");
// ...

      



Otherwise, the full response body is returned.

+7


source







All Articles