Java - Quickest way to check if URL exists

后端 未结 3 560
一个人的身影
一个人的身影 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: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();
            }
        }
    }
    

提交回复
热议问题