如何快速测试URL是否存在并且在java中有内容?

How to quickly test if a URL exists and has content in java?

我想测试一下是否存在数百个 URL,而我目前使用的方法太花时间了。这是我到目前为止发现的:

public static boolean checkURL(URL u)
{
HttpURLConnection connection = null;
try
{
  connection = (HttpURLConnection) u.openConnection();
  connection.setRequestMethod("HEAD");
  int code = connection.getResponseCode();
  System.out.println("" + code);
  // You can determine on HTTP return code received. 200 is success.
  if (code == 200)
  {
    return true;
  }
  else
  {
    return false;
  }
}
catch (MalformedURLException e)
{
  // TODO Auto-generated catch block
  // e.printStackTrace();
  System.out.println("error");
}
catch (IOException e)
{
  System.out.println("error2");
  // TODO Auto-generated catch block
  // e.printStackTrace();
}
finally
{
  if (connection != null)
  {
    connection.disconnect();
}
}

return false;
}

虽然这确实成功地找到了 URL 是否存在并具有内容,但它需要很长时间才能完成,程序通常需要五分钟以上的时间才能执行。有谁知道更有效的测试方法吗?

注意:不仅要测试url returns 200,还要测试网站不超时。

您的代码看起来不错,它应该是检查 url 的最简单方法。您可能希望在 HttpURLConnection 中添加 timeout

示例代码供参考。

enter code here
import java.net.HttpURLConnection;
import java.net.URL;

public class UrlChecker {
public static void main(String[] args) {
System.out.println(URLExists("http://slowwly.robertomurray.co.uk/delay/
3000/url/http://www.google.co.uk"));
}

public static boolean URLExists(String targetUrl) {
    HttpURLConnection urlConnection;
    try {
        urlConnection = (HttpURLConnection) new 
        URL(targetUrl).openConnection();
        urlConnection.setRequestMethod("HEAD");
        // Set timeouts 2000 in milliseconds and throw exception
        urlConnection.setConnectTimeout(2000);
        urlConnection.setReadTimeout(2000);
       /* Set timeouts 4000 in milliseconds and it should work as the url 
        should return back in 3 seconds.
        httpUrlConn.setConnectTimeout(4000);
        httpUrlConn.setReadTimeout(4000);
        */
        System.out.println("Response Code =>"+ 
        urlConnection.getResponseCode());
        System.out.println("Response Msg =>"+ 
        urlConnection.getResponseMessage());
        return (urlConnection.getResponseCode() == 
        HttpURLConnection.HTTP_OK);
    } catch (Exception e) {
        System.out.println("Exception => " + e.getMessage());
        return false;
    }
}
}