java URL 类 获取 相关 参数

原文:http://markjoker.iteye.com/blog/1734420


Java代码   收藏代码
  1. import java.net.MalformedURLException;  
  2. import java.net.URL;  
  3.   
  4. public class URLTest  
  5. {  
  6.     private static void testUrl(String path)  
  7.     {  
  8.         URL url = null;  
  9.         try  
  10.         {  
  11.             url = new URL(path);  
  12.         }  
  13.         catch (MalformedURLException e)  
  14.         {  
  15.             e.printStackTrace();  
  16.         }  
  17.         System.out.println("getAuthority:" + url.getAuthority());  
  18.         System.out.println("getDefaultPort:" + url.getDefaultPort());  
  19.         System.out.println("getFile:" + url.getFile());  
  20.         System.out.println("getHost:" + url.getHost());  
  21.         System.out.println("getPath:" + url.getPath());  
  22.         System.out.println("getPort:" + url.getPort());  
  23.         System.out.println("getProtocol:" + url.getProtocol());  
  24.         System.out.println("getQuery:" + url.getQuery());  
  25.         System.out.println("getRef:" + url.getRef());  
  26.         System.out.println("getUserInfo:" + url.getUserInfo());  
  27.     }  
  28.     public static void main(String[] args)  
  29.     {  
  30.         System.out.println("==http://www.baidu.com==");  
  31.         testUrl("http://www.baidu.com");  
  32.         System.out.println("==https://passport.baidu.com/==");  
  33.         testUrl("https://passport.baidu.com/");  
  34.     }  
  35. }  


结果:  
Java代码   收藏代码
  1. ==http://www.baidu.com==  
  2. getAuthority:www.baidu.com  
  3. getDefaultPort:80  
  4. getFile:  
  5. getHost:www.baidu.com  
  6. getPath:  
  7. getPort:-1  
  8. getProtocol:http  
  9. getQuery:null  
  10. getRef:null  
  11. getUserInfo:null  
  12. ==https://passport.baidu.com/==  
  13. getAuthority:passport.baidu.com  
  14. getDefaultPort:443  
  15. getFile:/  
  16. getHost:passport.baidu.com  
  17. getPath:/  
  18. getPort:-1  
  19. getProtocol:https  
  20. getQuery:null  
  21. getRef:null  
  22. getUserInfo:null  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值