- import java.net.URL;
- import java.net.HttpURLConnection;
- public class URLCheck {
- private boolean isConnect(String url) {
- boolean flag = false;
- int counts = 0;
- if (url == null || url.length() <= 0) {
- return flag;
- }
- while (counts < 5) {
- try {
- HttpURLConnection connection = (HttpURLConnection) new URL(url)
- .openConnection();
- int state = connection.getResponseCode();
- if (state == 200) {
- // String realurl = connection.getURL().toString();
- flag = true;
- }
- break;
- } catch (Exception ex) {
- counts++;
- continue;
- }
- }
- return flag;
- }
- public static void main(String[] args) {
- URLCheck check = new URLCheck();
- System.out.println(check.isConnect("http://gaojin.javaeye.com"));
- }
- }
Java检查URL是否有效
最新推荐文章于 2023-08-31 14:33:19 发布