zoukankan      html  css  js  c++  java
  • URI URL 的使用场合

    URL和URI

    URI:统一资源标识符,用于标识一个web资源,包含了两个部分。

    (1)URL:统一资源定位符。能够精确的定位数据的URI

    (2)URN:统一资源名称。除了URL的URI

    在java中URI和URL是分开的两个类,URI类专门用于解析,URL用于通信。

    URL

    1.URI分类

    绝对和相对:

    (1)绝对URI是指有确定的协议。比如http,ftp。后面以/进行分隔

    (2)相对URI是没有scheme的。

    透明和不透明:

    (1)不透明URI是不能够被解析的URI。不透明URI是绝对URI。scheme后面的部分不是以/进行分割。

    分层和不分层:

    (1)分层是绝对透明URI或相对URI。

    所有的网页端口都是80.

    2.URI的作用:

    (1)解析

    URI的格式:

    [scheme:]scheme-specific-part[#fragment]

    scheme表示用的协议,可以是http\https\ftp\file等。

    scheme-specific-part是其余部分。

    进一步细分:

    [scheme:][//authority][path][?query][#fragment]

    常用方法:

    • getScheme()获得scheme;
    • getSchemeSpecificPart()
    • getPath()
    • getAuthority()

    (2)相对标识符和绝对标识符的转换

    resolve和relative函数。

    示例代码:

    任务1:取得特定网址的html代码。

    任务2:分析地址信息。

    任务3:绝对地址和相对地址转换

    示例代码:

    任务1:取得特定网址的html代码。

    任务2:分析地址信息。

    任务3:绝对地址和相对地址转换

     

     

    public class URTest
    {
        public static void main(String[] arg0)
        {
            
            try
            {
                URL url = new URL("http://www.baidu.cn");  
                Scanner in = new Scanner(url.openStream());  
                while (in.hasNextLine()) {  
                    String str = in.nextLine();  
                    System.out.println(str);  
                } 
                //http://www.cnblogs.com/xilinch/archive/2012/09/03/2668613.html
      
                URI uri = new URI("http://www.cnblogs.com/xilinch");  
                System.out.println(uri.getScheme());  
                System.out.println(uri.getSchemeSpecificPart());  
                System.out.println(uri.getAuthority());  
                System.out.println(uri.getUserInfo());  
                System.out.println(uri.getHost());  
                System.out.println(uri.getPort());  
                System.out.println(uri.getPath());  
                System.out.println(uri.getQuery());  
                System.out.println(uri.getFragment());  
      
                String str = "/archive/2012/09/03/2668613.html";  
                URI combined = uri.resolve(str);// 根据uri的路径把str变成绝对地址  
                System.out.println(combined.getScheme()  
                        + combined.getSchemeSpecificPart());  
      
                URI relative = uri.relativize(new URI(str));  
                System.out.println(relative.getSchemeSpecificPart());
            }
            catch (MalformedURLException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            catch (URISyntaxException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }  
            
        }
    
    }
    

    结果输出:

    <!doctype html><html><head><meta http-equiv="Content-Type" content="text/html;charset=gb2312"><title>�ٶ�һ�£����֪��      </title><style>html{overflow-y:auto}body{font:12px arial;text-align:center;background:#fff}body,p,form,ul,li{margin:0;padding:0;list-style:none}body,form,#fm{position:relative}td{text-align:left}img{border:0}a{color:#00c}a:active{color:#f60}#u{color:#999;padding:4px 10px 5px 0;text-align:right}#u a{margin:0 5px}#u .reg{margin:0}#m{680px;margin:0 auto;}#nv a,#nv b,.btn,#lk{font-size:14px}#fm{padding-left:90px;text-align:left}input{border:0;padding:0}#nv{height:19px;font-size:16px;margin:0 0 4px;text-align:left;text-indent:117px;}.s_ipt_wr{418px;height:30px;display:inline-block;margin-right:5px;background:url(http://s1.bdstatic.com/r/www/img/i-1.0.0.png) no-repeat -304px 0;border:1px solid #b6b6b6;border-color:#9a9a9a #cdcdcd #cdcdcd #9a9a9a;vertical-align:top}.s_ipt{405px;height:22px;font:16px/22px arial;margin:5px 0 0 7px;background:#fff;outline:none;-webkit-appearance:none}.s_btn{95px;height:32px;padding-top:2px\9;font-size:14px;background:#ddd url(http://s1.bdstatic.com/r/www/img/i-1.0.0.png);cursor:pointer}.s_btn_h{background-position:-100px 0}.s_btn_wr{97px;height:34px;display:inline-block;background:url(http://s1.bdstatic.com/r/www/img/i-1.0.0.png) no-repeat -202px 0;*position:relative;z-index:0;vertical-align:top}#lg img{vertical-align:top;margin-bottom:3px}#lk{margin:33px 0}#lk span{font:14px "����"}#lm{height:60px}#lh{margin:16px 0 5px;word-spacing:3px}.tools{position:absolute;top:-4px;*top:10px;right:-13px;}#mHolder{62px;position:relative;z-index:296;display:none}#mCon{height:18px;line-height:18px;position:absolute;cursor:pointer;padding:0 18px 0 0;background:url(http://s1.bdstatic.com/r/www/img/bg-1.0.0.gif) no-repeat right -134px;background-position:right -136px\9}#mCon span{color:#00c;cursor:default;display:block}#mCon .hw{text-decoration:underline;cursor:pointer}#mMenu a{100%;height:100%;display:block;line-height:22px;text-indent:6px;text-decoration:none;filter:none\9}#mMenu,#user ul{box-shadow:1px 1px 2px #ccc;-moz-box-shadow:1px 1px 2px #ccc;-webkit-box-shadow:1px 1px 2px #ccc;filter: progid:DXImageTransform.Microsoft.Shadow(Strength=2, Direction=135, Color="#cccccc")\9;}#mMenu{56px;border:1px solid #9b9b9b;list-style:none;position:absolute;right:7px;top:28px;display:none;background:#fff}#mMenu a:hover{background:#ebebeb}#mMenu .ln{height:1px;background:#ebebeb;overflow:hidden;font-size:1px;line-height:1px;margin-top:-1px}#cp,#cp a{color:#77c}#seth{display:none;behavior:url(#default#homepage)}#setf{display:none;}#sekj{margin-left:14px;}</style>
    <script type="text/javascript">function h(obj){obj.style.behavior='url(#default#homepage)';var a = obj.setHomePage('http://www.baidu.com/');}</script></head>
    
    <body><div id="u"><a href="http://www.baidu.com/gaoji/preferences.html" name="tj_setting">��������</a>|<a href="https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" name="tj_login" id="lb" onclick="return false;">��¼</a><a href="https://passport.baidu.com/v2/?reg&regType=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" target="_blank" name="tj_reg" class="reg">ע��</a></div>
    <div id="m"><p id="lg"><img src="http://www.baidu.com/img/baidu_sylogo1.gif" width="270" height="129" ></p>
    <p id="nv"><a href="http://news.baidu.com">�� ��</a>��<b>�� ҳ</b>��<a href="http://tieba.baidu.com">�� ��</a>��<a href="http://zhidao.baidu.com">֪ ��</a>��<a href="http://mp3.baidu.com">MP3</a>��<a href="http://image.baidu.com">ͼ Ƭ</a>��<a href="http://video.baidu.com">�� Ƶ</a>��<a href="http://map.baidu.com">�� ͼ</a></p><div id="fm"><form name="f" action="/s"><span class="s_ipt_wr"><input type="text" name="wd" id="kw" maxlength="100" class="s_ipt"></span><input type="hidden" name="rsv_bp" value="0"><input type="hidden" name="rsv_spt" value="3"><span class="s_btn_wr"><input type="submit" value="�ٶ�һ��" id="su" class="s_btn" onmousedown="this.className='s_btn s_btn_h'" onmouseout="this.className='s_btn'"></span></form><span class="tools"><span id="mHolder"><div id="mCon"><span>���뷨</span></div></span></span><ul id="mMenu"><li><a href="#" name="ime_hw">��д</a></li><li><a href="#" name="ime_py">ƴ��</a></li><li class="ln"></li><li><a href="#" name="ime_cl">�ر�</a></li></ul></div>
    <p id="lk"><a href="http://baike.baidu.com">�ٿ�</a>��<a href="http://wenku.baidu.com">�Ŀ�</a>��<a href="http://www.hao123.com">hao123</a><span> | <a href="http://www.baidu.com/more/">���>></a></span></p><p id="lm"><a href="http://baiduworld.baidu.com/" title="" target="_blank" style="font-family: ����,Arial,Helvetica,sans-serif;">��ע100G�ƴ洢�ռ䣬��֤�ٶ��Ƶ�f��</a></p><p ><a id="seth" onClick="h(this)" href="/" onmousedown="return ns_c({'fm':'behs','tab':'homepage','pos':0})">�Ѱٶ���Ϊ��ҳ</a><a id="setf" href="http://www.baidu.com/cache/sethelp/index.html" onmousedown="return ns_c({'fm':'behs','tab':'favorites','pos':0})" target="_blank">�Ѱٶ���Ϊ��ҳ</a><span id="sekj"><a href="http://www.baidu.com/search/baidukuijie_mp.html" target="_blank" onmousedown="return ns_c({'fm':'behs','tab':'kuaijie','pos':1})">�Ѱٶ���ӵ�����</a></span></p>
    <p id="lh"><a href="http://e.baidu.com/?refer=888">����ٶ��ƹ�</a> | <a href="http://top.baidu.com">������ư�</a> | <a href="http://home.baidu.com">���ڰٶ�</a> | <a href="http://ir.baidu.com">About Baidu</a></p><p id="cp">©2012 Baidu <a href="/duty/">ʹ�ðٶ�ǰ�ض�</a> <a href="http://www.miibeian.gov.cn" target="_blank">��ICP֤030173��</a> <img src="http://www.baidu.com/cache/global/img/gs.gif"></p></div></body>
    
    <script>var bds={se:{},comm : {ishome : 1,sid : "",user : "",username : "",sugHost : "http://suggestion.baidu.com/su",loginAction : []}}</script><script type="text/javascript" src="http://s1.bdstatic.com/r/www/cache/global/js/home-1.3.js"></script><script>var bdUser = null;var w=window,d=document,n=navigator,k=d.f.wd,a=d.getElementById("nv").getElementsByTagName("a"),isIE=n.userAgent.indexOf("MSIE")!=-1&&!window.opera;(function(){if(/q=([^&]+)/.test(location.search)){k.value=decodeURIComponent(RegExp["\x241"])}})();if(n.cookieEnabled&&!/sug?=0/.test(d.cookie)){bds.se.sug();};function addEV(o, e, f){if(w.attachEvent){o.attachEvent("on" + e, f);}else if(w.addEventListener){ o.addEventListener(e, f, false);}}function G(id){return d.getElementById(id);}function ns_c(q){var p = encodeURIComponent(window.document.location.href), sQ = '', sV = '', mu='', img = window["BD_PS_C" + (new Date()).getTime()] = new Image();for (v in q) {sV = q[v];sQ += v + "=" + sV + "&";} mu= "&mu=" + p ;img.src = "http://nsclick.baidu.com/v.gif?pid=201&pj=www&rsv_sid=&" + sQ + "path="+p+"&t="+new Date().getTime();return true;}if(/\bbdime=[12]/.test(d.cookie)){document.write('<script src=http://s1.bdstatic.com/r/www/cache/ime/js/openime-1.0.0.js><\/script>');}(function(){var u = G("u").getElementsByTagName("a"), nv = G("nv").getElementsByTagName("a"), lk = G("lk").getElementsByTagName("a"), un = "";var tj_nv = ["news","tieba","zhidao","mp3","img","video","map"];var tj_lk = ["baike","wenku","hao123","more"];un = bds.comm.user == "" ? "" : bds.comm.user;function _addTJ(obj){addEV(obj, "mousedown", function(e){var e = e || window.event;var target = e.target || e.srcElement;ns_c({'fm':'behs','tab':target.name||'tj_user','un':encodeURIComponent(un)});});}for(var i = 0; i < u.length; i++){_addTJ(u[i]);}for(var i = 0; i < nv.length; i++){nv[i].name = 'tj_' + tj_nv[i];}for(var i = 0; i < lk.length; i++){lk[i].name = 'tj_' + tj_lk[i];}})();(function() {var links = {'tj_news':   ['word', 'http://news.baidu.com/ns?tn=news&cl=2&rn=20&ct=1&ie=utf-8'],'tj_tieba':  ['kw', 'http://tieba.baidu.com/f?ie=utf-8'],'tj_zhidao': ['word', 'http://zhidao.baidu.com/search?pn=0&rn=10&lm=0'],'tj_mp3':    ['word', 'http://mp3.baidu.com/m?f=ms&tn=baidump3&ct=134217728&rn=&lm=-1&pn=30&ie=utf-8'],'tj_img':    ['word', 'http://image.baidu.com/i?ct=201326592&cl=2&nc=1&lm=-1&st=-1&tn=baiduimage&istype=2&fm=&pv=&z=0&ie=utf-8'],'tj_video':  ['word', 'http://video.baidu.com/v?ct=301989888&s=25&ie=utf-8'],'tj_map':    ['wd', 'http://map.baidu.com/?newmap=1&ie=utf-8&s=s'],'tj_baike':  ['word', 'http://baike.baidu.com/search/word?pic=1&sug=1&enc=utf8'],'tj_wenku':  ['word', 'http://wenku.baidu.com/search?ie=utf-8']};var domArr = [G('nv'), G('lk')],kw = G('kw');for (var i = 0, l = domArr.length; i < l;  i++) {domArr[i].onmousedown = function(e) {e = e || window.event;var target = e.target || e.srcElement,name = target.getAttribute('name'),items = links[name],reg = new RegExp('^\\s+|\\s+\x24'),key = kw.value.replace(reg, '');if (items) {if (key.length > 0) {var wd = items[0], url = items[1],url = url + ( name === 'tj_map' ? encodeURIComponent('&' + wd + '=' + key)  : ( ( url.indexOf('?') > 0 ? '&' : '?' ) + wd + '=' + encodeURIComponent(key)  ) );target.href = url;} else {target.href = target.href.match(new RegExp('^http://.+\\.baidu\\.com'))[0];}}name && ns_c({'fm': 'behs','tab': name,'query': encodeURIComponent(key),'un': encodeURIComponent(bds.comm.user || '')    });};}})();addEV(w,"load",function(){k.focus()});w.onunload=function(){};</script><script type="text/javascript" src="http://s1.bdstatic.com/r/www/cache/global/js/tangram-1.3.4c1.0.js"></script><script type="text/javascript" src="http://s1.bdstatic.com/r/www/cache/user/js/u-1.3.3.js"></script><script>(function(){var C=G("lm").getElementsByTagName("A");for(var B=0,A=C.length;B<A;B++){var D=C[B];addEV(D,"mousedown",(function(E,F){return function(){ns_c({fm:"behs",tab:"bdlnk",p1:F+1,title:E.innerHTML,url:E.href})}})(D,B))}})();</script>
    
    
    
    </html>
    
    
    
    <!--ab2c87c184084e43-->
    http
    //www.cnblogs.com/xilinch
    www.cnblogs.com
    null
    www.cnblogs.com
    -1
    /xilinch
    null
    null
    http//www.cnblogs.com/archive/2012/09/03/2668613.html
    /archive/2012/09/03/2668613.html
    

     

  • 相关阅读:
    python3 pyinstaller
    python3 random
    python3 turtle
    产生一个序列的所有排列组合
    蒙特卡洛算法
    lightoj 1014
    UVA11426
    nginx 配置本地https(免费证书)
    ElementUI
    Airbnb 代码规范
  • 原文地址:https://www.cnblogs.com/xilinch/p/2668638.html
Copyright © 2011-2022 走看看