Java - Quickest way to check if URL exists

后端 未结 3 537
一个人的身影
一个人的身影 2021-01-03 21:49

Hi I am writing a program that goes through many different URLs and just checks if they exist or not. I am basically checking if the error code returned is 404 or not. Howev

相关标签:
3条回答
  • 2021-01-03 22:16

    Seems you can set the timeout property, make sure it is acceptable. And if you have many urls to test, do them parallelly, it will be much faster. Hope this will be helpful.

    0 讨论(0)
  • 2021-01-03 22:21

    Try to ask the next DNS Server

    class DNSLookup
    {
        public static void main(String args[])
        {
            String host = "stackoverflow.com";
            try
            {
                InetAddress inetAddress = InetAddress.getByName(host);
                // show the Internet Address as name/address
                System.out.println(inetAddress.getHostName() + " " + inetAddress.getHostAddress());
            }
            catch (UnknownHostException exception)
            {
                System.err.println("ERROR: Cannot access '" + host + "'");
            }
            catch (NamingException exception)
            {
                System.err.println("ERROR: No DNS record for '" + host + "'");
                exception.printStackTrace();
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-03 22:28

    Try sending a "HEAD" request instead of get request. That should be faster since the response body is not downloaded.

    huc.setRequestMethod("HEAD");
    

    Again instead of checking if response status is not 400, check if it is 200. That is check for positive instead of negative. 404,403,402.. all 40x statuses are nearly equivalent to invalid non-existant url.

    You may make use of multi-threading to make it even faster.

    0 讨论(0)
提交回复
热议问题