Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Get page headers then close connection

I'd like to only get page headers for a url without downloading the page content. This is what I'm using right now:

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

I'm not sure if this is getting the headers then quitting or what its doing.

like image 702
orourkedd Avatar asked Jul 03 '26 05:07

orourkedd


1 Answers

Send a HEAD request instead of a GET request (which is the default method).

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

Otherwise the full response body is been returned.

like image 57
BalusC Avatar answered Jul 04 '26 19:07

BalusC