如何快速测试 URL 是否存在并在 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返回200,而且网站没有超时。


吃鸡游戏
浏览 229回答 1
1回答

红颜莎娜

您的代码看起来不错,它应该是检查 url 的最简单方法。您可能希望在 HttpURLConnection中添加超时。示例代码供参考。enter code hereimport 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;    }}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java