public class Demo {
/*
* InetAddress
* 此类表示互联网协议 (IP) 地址。
* 封装计算机的ip地址 ,没有端口
*/
public static void main(String[] args) throws UnknownHostException {
//创建IP对象
//InetAddress ip = InetAddress.getByName("192.168.3.134");
//InetAddress ip = InetAddress.getByName("PC-20181122ZRPS");
//InetAddress ip = InetAddress.getByName("127.0.0.1");
//InetAddress ip = InetAddress.getByName("localhost");
InetAddress ip = InetAddress.getByName("www.taobao.com");//可以获取网络地址
//InetAddress ip = InetAddress.getLocalHost();//只能获取本机的ip
//获取主机地址
System.out.println(ip.getHostAddress());
//获取主机名
System.out.println(ip.getHostName());
}
}
import java.net.MalformedURLException;
import java.net.URL;
public class Demo {
/*
* https://www.baidu.com/s?wd=java
* http://192.168.3.134:8080/whsxt/login.jsp
* URL:统一资源定位符
* 1 协议 http
* 2 存放资源的主机域名 192.168.3.134
* 3 端口号 8080
* 4 资源文件名 whsxt/login.jsp
*
*/
public static void main(String[] args) throws MalformedURLException {
//1:创建URL
//URL url = new URL("http", "192.168.3.134", 12306, "/whsxt/login.jsp");
//URL url = new URL("https://www.bjsxt.com");
URL url = new URL("http://192.168.3.134:12306/whsxt/login.jsp?name=张三&age=20&sex=男");
System.out.println(url.getProtocol()); //获取协议
System.out.println(url.getHost());//获取主机
System.out.println(url.getPort());//获取端口号
System.out.println(url.getFile()); //获取资源文件
System.out.println(url.getQuery());//获取传参
}
}