网络编程_URL_基本用法
完整的URL:协议、存放资源的主机域名、端口号、资源文件名、锚点。
?号后面跟参数,#号后面跟锚点。
package com.sxt.loc;
import java.net.MalformedURLException;
import java.net.URL;
/**
* URL: 统一资源定位器,区分资源,互联网三大基石之一(html http)。
* 1、协议
* 2、域名、计算机
* 3、端口: 默认80
* 4、请求资源
* http://www.baidu.com:80/index.html?uname=shsxt&age=18#a
*
* @author
*
*/
public class URLTest01 {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://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()); //URI
//参数
System.out.println("参数:"+url.getQuery());
//锚点
System.out.println("锚点:"+url.getRef());
}
}