1 import java.net.*; 2 import java.io.*; 3 4 public class URLDemo 5 { 6 public static void main(String [] args) 7 { 8 try 9 { 10 URL url = new URL("http://www.runoob.com/index.html?language=cn#j2se"); 11 System.out.println("URL 为:" + url.toString());//URL 为:http://www.runoob.com/index.html?language=cn#j2se 12 System.out.println("协议为:" + url.getProtocol());//协议为:http 13 System.out.println("验证信息:" + url.getAuthority());//验证信息:www.runoob.com 14 System.out.println("文件名及请求参数:" + url.getFile());//文件名及请求参数:/index.html?language=cn 15 System.out.println("主机名:" + url.getHost());//主机名:www.runoob.com 16 System.out.println("路径:" + url.getPath());//路径:/index.html 17 System.out.println("端口:" + url.getPort());//端口:-1 18 System.out.println("默认端口:" + url.getDefaultPort());//默认端口:80 19 System.out.println("请求参数:" + url.getQuery());//请求参数:language=cn 20 System.out.println("定位位置:" + url.getRef());//定位位置:j2se 21 }catch(IOException e) 22 { 23 e.printStackTrace(); 24 } 25 } 26 }