package test.ping;
import java.io.IOException;
import java.net.InetAddress;
import java.net.URL;
public class TestGetIp {
public static void main(String[] args) throws IOException {
String href = "http://www.baidu.com";
int port = parsePort(href);
String host = parseHost(href);
String address = parseIp(host);
System.out.println("host=" + host);
System.out.println("port=" + port);
System.out.println("address=" + address);
}
public static int parsePort(String href) throws IOException {
URL url = new URL(href);
int port = url.getPort();
if (port < 0) {
port = url.getDefaultPort();
}
return port;
}
public static String parseHost(String href) throws IOException {
URL url = new URL(href);
String host = url.getHost();
return host;
}
public static String parseIp(String host) throws IOException {
InetAddress inetAddress = InetAddress.getByName(host);
String address = inetAddress.getHostAddress();
return address;
}
}