开发者社区> 问答> 正文

Java程序可ping数千个URL

我想编写一个程序来ping至少10000个URL。我写了一个小程序,发现它没有我预期的那么快。

对100个网址执行Ping操作需要3-4分钟。

有人有任何建议以更好的方式做到这一点。

private static Map<String, String> findUnreachableUrls(Set urls) { Map<String, String> badUrls = new TreeMap<>(); for (String url : urls) { HttpURLConnection connection; try { connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestMethod("HEAD"); connection.connect(); int responseCode = connection.getResponseCode(); if (responseCode != 200 || responseCode != 302) { badUrls.put(url, Integer.toString(responseCode)); } } catch (IOException e) { badUrls.put(url, e.getMessage()); }

    }
    return badUrls;
}

展开
收起
被纵养的懒猫 2019-10-08 17:09:06 254 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载