| | |
| | | package com.se.system.utils; |
| | | |
| | | import java.net.HttpURLConnection; |
| | | import java.net.InetAddress; |
| | | import java.net.URL; |
| | | import java.net.*; |
| | | |
| | | @SuppressWarnings("ALL") |
| | | public class ConnectUtils { |
| | |
| | | try { |
| | | InetAddress inetAddress = InetAddress.getByName(ip); |
| | | |
| | | return inetAddress.isReachable(1000); |
| | | return inetAddress.isReachable(1500); |
| | | } catch (Exception ex) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | public static boolean getUrl(String uri, String method) { |
| | | 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(method); |
| | | con.setRequestMethod(StringUtils.isEmpty(method) ? "GET" : method); |
| | | con.setConnectTimeout(1000); |
| | | con.setReadTimeout(1000); |
| | | |