Check if resource exists

suggest change
 * Checks if a resource exists by sending a HEAD-Request.
 * @param url The url of a resource which has to be checked.
 * @return true if the response code is 200 OK.
public static final boolean checkIfResourceExists(URL url) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    int code = conn.getResponseCode();
    return code == 200;


If you are just checking if a resource exists, it better to use a HEAD request than a GET. This avoids the overhead of transferring the resource.

Note that the method only returns true if the response code is 200. If you anticipate redirect (i.e. 3XX) responses, then the method may need to be enhanced to honor them.


checkIfResourceExists(new URL("")); // true
checkIfResourceExists(new URL("")); // false

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents