URLTest.java
import java.net.MalformedURLException;
import java.net.URL;
public class URLTest {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("https://www.baidu.com:80/index.html?uname=shsxt&age=18#a");
System.out.println("协议:" + url.getProtocol());
System.out.println("域名IP:" + url.getHost());
System.out.println("端口:" + url.getPort());
System.out.println("请求资源1:" + url.getFile());
System.out.println("请求资源2:" + url.getPath());
System.out.println("参数:" + url.getQuery());
System.out.println("锚点:" + url.getRef());
}
}
运行结果:
协议:https
域名IP:www.baidu.com
端口:80
请求资源1:/index.html?uname=shsxt&age=18
请求资源2:/index.html
参数:uname=shsxt&age=18
锚点:a