Check if url exists JAVA
<p><br> I'm trying to check if the url entered by the user actually exists. 
Below is what I have tried.</p>

<pre><code>public static Boolean checkURLExists(String urlName) 
{
    Boolean urlCheck=false;
    try{
        URL url = new URL(urlName);
        HttpURLConnection.setFollowRedirects(false);
        HttpURLConnection huc = (HttpURLConnection) url.openConnection();
        huc.setRequestMethod("GET");
        int responseCode = huc.getResponseCode();
        String responseMessage = huc.getResponseMessage();
        char a=String.valueOf(Math.abs((long)huc.getResponseCode())).charAt(0);
        if ((a == '2' || a == '3')&amp;&amp; (responseMessage.equalsIgnoreCase("ok")||responseMessage.equalsIgnoreCase("found")||responseMessage.equalsIgnoreCase("redirect"))) {
            System.out.println("GOOD "+responseCode+" - "+a);
            urlCheck=true;
        } else {
            System.out.println("BAD "+responseCode+" - "+a);
        }
    }catch(Exception e){
                    e.printStackTrace();
    }
    return urlCheck;
}
</code></pre>

<p>The issue with the above code is that it returns <a href="http://www.gmail.com" rel="nofollow">http://www.gmail.com</a> or <a href="http://www.yahoo.co.in" rel="nofollow">http://www.yahoo.co.in</a> etc. as invalid URLs with response code 301 &amp; response message "Moved permanently" but they actually redirects to other url, Is there any way to detect that the url when entered in browser will open a page?</p>

<p>Thank you.</p>