1
13693261870
2024-12-11 c6550a4d9bd69e59e9bb6ac6ad740e509edbd215
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.se.system.utils;
 
import java.net.*;
 
@SuppressWarnings("ALL")
public class ConnectUtils {
    public static boolean pingIp(String ip) {
        try {
            InetAddress inetAddress = InetAddress.getByName(ip);
 
            return inetAddress.isReachable(1500);
        } catch (Exception ex) {
            return false;
        }
    }
 
    public static boolean isReachable(String addr) {
        try {
            String[] strs = addr.split(":");
            InetSocketAddress socketAddress = new InetSocketAddress(strs[0], strs.length < 2 || StringUtils.isEmpty(strs[1]) ? 80 : Integer.parseInt(strs[1]));
 
            Socket socket = new Socket();
            socket.connect(socketAddress, 1500);
            socket.close();
 
            return true;
        } catch (Exception ex) {
            return false;
        }
    }
 
    public static boolean testUrl(String uri, String method) {
        try {
            URL url = new URL(uri);
 
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod(StringUtils.isEmpty(method) ? "GET" : method);
            con.setConnectTimeout(1000);
            con.setReadTimeout(1000);
 
            int code = con.getResponseCode();
 
            return code >= 200 && code <= 399;
        } catch (Exception ex) {
            return false;
        }
    }
}