title: Preferred Java way to ping an HTTP URL for availability
body: <p>I need a monitor class that regularly checks whether a given HTTP URL is available. I can take care of the "regularly" part using the Spring TaskExecutor abstraction, so that's not the topic here. The question is: <strong>What is the preferred way to ping a URL in java?</strong></p>
  
  <p>Here is my current code as a starting point:</p>
  
  <pre><code>try {
      final URLConnection connection = new URL(url).openConnection();
      connection.connect();
      LOG.info("Service " + url + " available, yeah!");
      available = true;
  } catch (final MalformedURLException e) {
      throw new IllegalStateException("Bad URL: " + url, e);
  } catch (final IOException e) {
      LOG.info("Service " + url + " unavailable, oh no!", e);
      available = false;
  }
  </code></pre>
  
  <ol>
  <li>Is this any good at all (will it do what I want)?</li>
  <li>Do I have to somehow close the connection?</li>
  <li>I suppose this is a <code>GET</code> request. Is there a way to send <code>HEAD</code> instead?</li>
  </ol>