title: get http response code from a url
body: <p>I want to write a Java program which will hit a URL and will print the status code(i.e., 200, 404, etc.). I am doing this using the <code>HttpUrlConnection</code> api, but it only shows the exception, and does not print the status code.</p>
  
  <pre><code>URL url = new URL("https://abc.com/test.html");
  HttpURLConnection connection = (HttpURLConnection)url.openConnection();
  connection.setRequestMethod("GET");
  connection.connect();
  
  int code = connection.getResponseCode();  
  System.out.println("code: "+code);
  </code></pre>