URL鍘熺悊銆乁RL缂栫爜銆乁RL鐗规畩瀛楃

杞浇鈥斺�斺�斺��

閫氬父濡傛灉涓�鏍蜂笢瑗块渶瑕佺紪鐮侊紝璇存槑杩欐牱涓滆タ骞朵笉閫傚悎浼犺緭銆傚師鍥犲绉嶅鏍凤紝濡係ize杩囧ぇ锛屽寘鍚殣绉佹暟鎹紝瀵逛簬Url鏉ヨ锛屼箣鎵�浠ヨ杩涜缂栫爜锛屾槸鍥犱负Url涓湁浜涘瓧绗︿細寮曡捣姝т箟銆�

渚嬪锛孶rl鍙傛暟瀛楃涓蹭腑浣跨敤key=value閿�煎杩欐牱鐨勫舰寮忔潵浼犲弬锛岄敭鍊煎涔嬮棿浠�&绗﹀彿鍒嗛殧锛屽/s?q=abc&ie=utf-8銆傚鏋滀綘鐨剉alue瀛楃涓蹭腑鍖呭惈浜�=鎴栬��&锛岄偅涔堝娍蹇呬細閫犳垚鎺ユ敹Url鐨勬湇鍔″櫒瑙f瀽閿欒锛屽洜姝ゅ繀椤诲皢寮曡捣姝т箟鐨�&鍜�=绗﹀彿杩涜杞箟锛屼篃灏辨槸瀵瑰叾杩涜缂栫爜銆�

鍙堝锛孶rl鐨勭紪鐮佹牸寮忛噰鐢ㄧ殑鏄疉SCII鐮侊紝鑰屼笉鏄疷nicode锛岃繖涔熷氨鏄浣犱笉鑳藉湪Url涓寘鍚换浣曢潪ASCII瀛楃锛屼緥濡備腑鏂囥�傚惁鍒欏鏋滃鎴风娴忚鍣ㄥ拰鏈嶅姟绔祻瑙堝櫒鏀寔鐨勫瓧绗﹂泦涓嶅悓鐨勬儏鍐典笅锛屼腑鏂囧彲鑳戒細閫犳垚闂銆�

Url缂栫爜鐨勫師鍒欙細灏辨槸浣跨敤瀹夊叏鐨勫瓧绗︼紙娌℃湁鐗规畩鐢ㄩ�旀垨鑰呯壒娈婃剰涔夌殑鍙墦鍗板瓧绗︼級鍘昏〃绀洪偅浜涗笉瀹夊叏鐨勫瓧绗︺��

棰勫鐭ヨ瘑锛歎RI鏄粺涓�璧勬簮鏍囪瘑鐨勬剰鎬濓紝閫氬父鎴戜滑鎵�璇寸殑URL鍙槸URI鐨勪竴绉嶃�傚吀鍨婾RL鐨勬牸寮忓涓嬫墍绀恒�備笅闈㈡彁鍒扮殑URL缂栫爜锛屽疄闄呬笂搴旇鎸囩殑鏄疷RI缂栫爜銆�

foo://example.com:8042/over/there?name=ferret#nose

\_/ \______________/ \________/\_________/ \__/

|聽聽聽聽聽聽聽聽 |聽聽聽聽聽聽聽聽聽聽聽聽聽 |聽聽聽聽聽聽聽聽 |聽聽聽聽聽聽聽 |

scheme聽聽聽聽 authority 聽 聽 聽 聽 聽 聽 聽 聽path 聽 聽 聽query聽聽 fragment

鍝簺瀛楃闇�瑕佺紪鐮�

RFC3986鏂囨。瑙勫畾锛孶rl涓彧鍏佽鍖呭惈鑻辨枃瀛楁瘝锛坅-zA-Z锛夈�佹暟瀛楋紙0-9锛夈��-_.~4涓壒娈婂瓧绗︿互鍙婃墍鏈変繚鐣欏瓧绗︺�俁FC3986鏂囨。瀵筓rl鐨勭紪瑙g爜闂鍋氬嚭浜嗚缁嗙殑寤鸿锛屾寚鍑轰簡鍝簺瀛楃闇�瑕佽缂栫爜鎵嶄笉浼氬紩璧稶rl璇箟鐨勮浆鍙橈紝浠ュ強瀵逛负浠�涔堣繖浜涘瓧绗﹂渶瑕佺紪鐮佸仛鍑轰簡鐩稿簲鐨勮В閲娿��

US-ASCII瀛楃闆嗕腑娌℃湁瀵瑰簲鐨勫彲鎵撳嵃瀛楃锛歎rl涓彧鍏佽浣跨敤鍙墦鍗板瓧绗︺�俇S-ASCII鐮佷腑鐨�10-7F瀛楄妭鍏ㄩ兘琛ㄧず鎺у埗瀛楃锛岃繖浜涘瓧绗﹂兘涓嶈兘鐩存帴鍑虹幇鍦║rl涓�傚悓鏃讹紝瀵逛簬80-FF瀛楄妭锛圛SO-8859-1锛夛紝鐢变簬宸茬粡瓒呭嚭浜哢S-ACII瀹氫箟鐨勫瓧鑺傝寖鍥达紝鍥犳涔熶笉鍙互鏀惧湪Url涓��

淇濈暀瀛楃锛歎rl鍙互鍒掑垎鎴愯嫢骞蹭釜缁勪欢锛屽崗璁�佷富鏈恒�佽矾寰勭瓑銆傛湁涓�浜涘瓧绗︼紙:/?#[]@锛夋槸鐢ㄤ綔鍒嗛殧涓嶅悓缁勪欢鐨勩�備緥濡傦細鍐掑彿鐢ㄤ簬鍒嗛殧鍗忚鍜屼富鏈猴紝/鐢ㄤ簬鍒嗛殧涓绘満鍜岃矾寰勶紝?鐢ㄤ簬鍒嗛殧璺緞鍜屾煡璇㈠弬鏁帮紝绛夌瓑銆傝繕鏈変竴浜涘瓧绗︼紙!$&'()*+,;=锛夌敤浜庡湪姣忎釜缁勪欢涓捣鍒板垎闅斾綔鐢ㄧ殑锛屽=鐢ㄤ簬琛ㄧず鏌ヨ鍙傛暟涓殑閿�煎锛�&绗﹀彿鐢ㄤ簬鍒嗛殧鏌ヨ澶氫釜閿�煎銆傚綋缁勪欢涓殑鏅�氭暟鎹寘鍚繖浜涚壒娈婂瓧绗︽椂锛岄渶瑕佸鍏惰繘琛岀紪鐮併��

RFC3986涓寚瀹氫簡浠ヤ笅瀛楃涓轰繚鐣欏瓧绗︼細! * ' ( ) ; : @ & = + $ , / ? # [ ]

涓嶅畨鍏ㄥ瓧绗︼細杩樻湁涓�浜涘瓧绗︼紝褰撲粬浠洿鎺ユ斁鍦║rl涓殑鏃跺�欙紝鍙兘浼氬紩璧疯В鏋愮▼搴忕殑姝т箟銆傝繖浜涘瓧绗﹁瑙嗕负涓嶅畨鍏ㄥ瓧绗︼紝鍘熷洜鏈夊緢澶氥��

绌烘牸锛歎rl鍦ㄤ紶杈撶殑杩囩▼锛屾垨鑰呯敤鎴峰湪鎺掔増鐨勮繃绋嬶紝鎴栬�呮枃鏈鐞嗙▼搴忓湪澶勭悊Url鐨勮繃绋嬶紝閮芥湁鍙兘寮曞叆鏃犲叧绱ц鐨勭┖鏍硷紝鎴栬�呭皢閭d簺鏈夋剰涔夌殑绌烘牸缁欏幓鎺夈��

寮曞彿浠ュ強<>锛氬紩鍙峰拰灏栨嫭鍙烽�氬父鐢ㄤ簬鍦ㄦ櫘閫氭枃鏈腑璧峰埌鍒嗛殧Url鐨勪綔鐢�

#锛氶�氬父鐢ㄤ簬琛ㄧず涔︾鎴栬�呴敋鐐�

%锛氱櫨鍒嗗彿鏈韩鐢ㄤ綔瀵逛笉瀹夊叏瀛楃杩涜缂栫爜鏃朵娇鐢ㄧ殑鐗规畩瀛楃锛屽洜姝ゆ湰韬渶瑕佺紪鐮�

{}|\^[]`~锛氭煇涓�浜涚綉鍏虫垨鑰呬紶杈撲唬鐞嗕細绡℃敼杩欎簺瀛楃

闇�瑕佹敞鎰忕殑鏄紝瀵逛簬Url涓殑鍚堟硶瀛楃锛岀紪鐮佸拰涓嶇紪鐮佹槸绛変环鐨勶紝浣嗘槸瀵逛簬涓婇潰鎻愬埌鐨勮繖浜涘瓧绗︼紝濡傛灉涓嶇粡杩囩紪鐮侊紝閭d箞瀹冧滑鏈夊彲鑳戒細閫犳垚Url璇箟鐨勪笉鍚屻�傚洜姝ゅ浜嶶rl鑰岃█锛屽彧鏈夋櫘閫氳嫳鏂囧瓧绗﹀拰鏁板瓧锛岀壒娈婂瓧绗�$-_.+!*'()杩樻湁淇濈暀瀛楃锛屾墠鑳藉嚭鐜板湪鏈粡缂栫爜鐨刄rl涔嬩腑銆傚叾浠栧瓧绗﹀潎闇�瑕佺粡杩囩紪鐮佷箣鍚庢墠鑳藉嚭鐜板湪Url涓��

浣嗘槸鐢变簬鍘嗗彶鍘熷洜锛岀洰鍓嶅皻瀛樺湪涓�浜涗笉鏍囧噯鐨勭紪鐮佸疄鐜般�備緥濡傚浜巭绗﹀彿锛岃櫧鐒禦FC3986鏂囨。瑙勫畾锛屽浜庢尝娴鍙穨锛屼笉闇�瑕佽繘琛孶rl缂栫爜锛屼絾鏄繕鏄湁寰堝鑰佺殑缃戝叧鎴栬�呬紶杈撲唬鐞嗕細杩涜缂栫爜銆�

濡備綍瀵筓rl涓殑闈炴硶瀛楃杩涜缂栫爜

Url缂栫爜閫氬父涔熻绉颁负鐧惧垎鍙风紪鐮侊紙Url Encoding锛宎lso known as percent-encoding锛夛紝鏄洜涓哄畠鐨勭紪鐮佹柟寮忛潪甯哥畝鍗曪紝浣跨敤%鐧惧垎鍙峰姞涓婁袱浣嶇殑瀛楃鈥斺��0123456789ABCDEF鈥斺�斾唬琛ㄤ竴涓瓧鑺傜殑鍗佸叚杩涘埗褰㈠紡銆俇rl缂栫爜榛樿浣跨敤鐨勫瓧绗﹂泦鏄疷S-ASCII銆備緥濡俛鍦║S-ASCII鐮佷腑瀵瑰簲鐨勫瓧鑺傛槸0x61锛岄偅涔圲rl缂栫爜涔嬪悗寰楀埌鐨勫氨鏄�%61锛屾垜浠湪鍦板潃鏍忎笂杈撳叆http://g.cn/search?q=%61%62%63锛屽疄闄呬笂灏辩瓑鍚屼簬鍦╣oogle涓婃悳绱bc浜嗐�傚張濡侤绗﹀彿鍦ˋSCII瀛楃闆嗕腑瀵瑰簲鐨勫瓧鑺備负0x40锛岀粡杩嘦rl缂栫爜涔嬪悗寰楀埌鐨勬槸%40銆�

瀵逛簬闈濧SCII瀛楃锛岄渶瑕佷娇鐢ˋSCII瀛楃闆嗙殑瓒呴泦杩涜缂栫爜寰楀埌鐩稿簲鐨勫瓧鑺傦紝鐒跺悗瀵规瘡涓瓧鑺傛墽琛岀櫨鍒嗗彿缂栫爜銆傚浜嶶nicode瀛楃锛孯FC鏂囨。寤鸿浣跨敤utf-8瀵瑰叾杩涜缂栫爜寰楀埌鐩稿簲鐨勫瓧鑺傦紝鐒跺悗瀵规瘡涓瓧鑺傛墽琛岀櫨鍒嗗彿缂栫爜銆傚"涓枃"浣跨敤UTF-8瀛楃闆嗗緱鍒扮殑瀛楄妭涓�0xE4 0xB8 0xAD 0xE6 0x96 0x87锛岀粡杩嘦rl缂栫爜涔嬪悗寰楀埌"%E4%B8%AD%E6%96%87"銆�

濡傛灉鏌愪釜瀛楄妭瀵瑰簲鐫�ASCII瀛楃闆嗕腑鐨勬煇涓潪淇濈暀瀛楃锛屽垯姝ゅ瓧鑺傛棤闇�浣跨敤鐧惧垎鍙疯〃绀恒�備緥濡�"Url缂栫爜"锛屼娇鐢║TF-8缂栫爜寰楀埌鐨勫瓧鑺傛槸0x55 0x72 0x6C 0xE7 0xBC 0x96 0xE7 0xA0 0x81锛岀敱浜庡墠涓変釜瀛楄妭瀵瑰簲鐫�ASCII涓殑闈炰繚鐣欏瓧绗�"Url"锛屽洜姝よ繖涓変釜瀛楄妭鍙互鐢ㄩ潪淇濈暀瀛楃"Url"琛ㄧず銆傛渶缁堢殑Url缂栫爜鍙互绠�鍖栨垚"Url%E7%BC%96%E7%A0%81" 锛屽綋鐒讹紝濡傛灉浣犵敤"%55%72%6C%E7%BC%96%E7%A0%81"涔熸槸鍙互鐨勩��

鐢变簬鍘嗗彶鐨勫師鍥狅紝鏈変竴浜沀rl缂栫爜瀹炵幇骞朵笉瀹屽叏閬靛惊杩欐牱鐨勫師鍒欙紝涓嬮潰浼氭彁鍒般��

Javascript涓殑escape, encodeURI鍜宔ncodeURIComponent鐨勫尯鍒�

JavaScript涓彁渚涗簡3瀵瑰嚱鏁扮敤鏉ュUrl缂栫爜浠ュ緱鍒板悎娉曠殑Url锛屽畠浠垎鍒槸escape / unescape, encodeURI / decodeURI鍜宔ncodeURIComponent / decodeURIComponent銆傜敱浜庤В鐮佸拰缂栫爜鐨勮繃绋嬫槸鍙�嗙殑锛屽洜姝よ繖閲屽彧瑙i噴缂栫爜鐨勮繃绋嬨��

杩欎笁涓紪鐮佺殑鍑芥暟鈥斺�攅scape锛宔ncodeURI锛宔ncodeURIComponent鈥斺�旈兘鏄敤浜庡皢涓嶅畨鍏ㄤ笉鍚堟硶鐨刄rl瀛楃杞崲涓哄悎娉曠殑Url瀛楃琛ㄧず锛屽畠浠湁浠ヤ笅鍑犱釜涓嶅悓鐐广��

瀹夊叏瀛楃涓嶅悓锛�

涓嬮潰鍒楀嚭浜嗚繖涓変釜鍑芥暟鐨勫畨鍏ㄥ瓧绗︼紙鍗冲嚱鏁颁笉浼氬杩欎簺瀛楃杩涜缂栫爜锛�

escape锛�69涓級锛�*/@+-._0-9a-zA-Z

encodeURI锛�82涓級锛�!#$&'()*+,/:;=?@-._~0-9a-zA-Z

encodeURIComponent锛�71涓級锛�!'()*-._~0-9a-zA-Z

鍏煎鎬т笉鍚岋細escape鍑芥暟鏄粠JavaScript1.0鐨勬椂鍊欏氨瀛樺湪浜嗭紝鍏朵粬涓や釜鍑芥暟鏄湪Javascript 1.5鎵嶅紩鍏ョ殑銆備絾鏄敱浜嶫avascript 1.5宸茬粡闈炲父鏅強浜嗭紝鎵�浠ュ疄闄呬笂浣跨敤encodeURI鍜宔ncodeURIComponent骞朵笉浼氭湁浠�涔堝吋瀹规�ч棶棰樸��

瀵筓nicode瀛楃鐨勭紪鐮佹柟寮忎笉鍚岋細杩欎笁涓嚱鏁板浜嶢SCII瀛楃鐨勭紪鐮佹柟寮忕浉鍚岋紝鍧囨槸浣跨敤鐧惧垎鍙�+涓や綅鍗佸叚杩涘埗瀛楃鏉ヨ〃绀恒�備絾鏄浜嶶nicode瀛楃锛宔scape鐨勭紪鐮佹柟寮忔槸%uxxxx锛屽叾涓殑xxxx鏄敤鏉ヨ〃绀簎nicode瀛楃鐨�4浣嶅崄鍏繘鍒跺瓧绗︺�傝繖绉嶆柟寮忓凡缁忚W3C搴熷純浜嗐�備絾鏄湪ECMA-262鏍囧噯涓粛鐒朵繚鐣欑潃escape鐨勮繖绉嶇紪鐮佽娉曘�俥ncodeURI鍜宔ncodeURIComponent鍒欎娇鐢║TF-8瀵归潪ASCII瀛楃杩涜缂栫爜锛岀劧鍚庡啀杩涜鐧惧垎鍙风紪鐮併�傝繖鏄疪FC鎺ㄨ崘鐨勩�傚洜姝ゅ缓璁敖鍙兘鐨勪娇鐢ㄨ繖涓や釜鍑芥暟鏇夸唬escape杩涜缂栫爜銆�

閫傜敤鍦哄悎涓嶅悓锛歟ncodeURI琚敤浣滃涓�涓畬鏁寸殑URI杩涜缂栫爜锛岃�宔ncodeURIComponent琚敤浣滃URI鐨勪竴涓粍浠惰繘琛岀紪鐮併�備粠涓婇潰鎻愬埌鐨勫畨鍏ㄥ瓧绗﹁寖鍥磋〃鏍兼潵鐪嬶紝鎴戜滑浼氬彂鐜帮紝encodeURIComponent缂栫爜鐨勫瓧绗﹁寖鍥磋姣攅ncodeURI鐨勫ぇ銆傛垜浠笂闈㈡彁鍒拌繃锛屼繚鐣欏瓧绗︿竴鑸槸鐢ㄦ潵鍒嗛殧URI缁勪欢锛堜竴涓猆RI鍙互琚垏鍓叉垚澶氫釜缁勪欢锛屽弬鑰冮澶囩煡璇嗕竴鑺傦級鎴栬�呭瓙缁勪欢锛堝URI涓煡璇㈠弬鏁扮殑鍒嗛殧绗︼級锛屽锛氬彿鐢ㄤ簬鍒嗛殧scheme鍜屼富鏈猴紝?鍙风敤浜庡垎闅斾富鏈哄拰璺緞銆傜敱浜巈ncodeURI鎿嶇旱鐨勫璞℃槸涓�涓畬鏁寸殑鐨刄RI锛岃繖浜涘瓧绗﹀湪URI涓湰鏉ュ氨鏈夌壒娈婄敤閫旓紝鍥犳杩欎簺淇濈暀瀛楃涓嶄細琚玡ncodeURI缂栫爜锛屽惁鍒欐剰涔夊氨鍙樹簡銆�

缁勪欢鍐呴儴鏈夎嚜宸辩殑鏁版嵁琛ㄧず鏍煎紡锛屼絾鏄繖浜涙暟鎹唴閮ㄤ笉鑳藉寘鍚湁鍒嗛殧缁勪欢鐨勪繚鐣欏瓧绗︼紝鍚﹀垯灏变細瀵艰嚧鏁翠釜URI涓粍浠剁殑鍒嗛殧娣蜂贡銆傚洜姝ゅ浜庡崟涓粍浠朵娇鐢╡ncodeURIComponent锛岄渶瑕佺紪鐮佺殑瀛楃灏辨洿澶氫簡銆�

琛ㄥ崟鎻愪氦

褰揌tml鐨勮〃鍗曡鎻愪氦鏃讹紝姣忎釜琛ㄥ崟鍩熼兘浼氳Url缂栫爜涔嬪悗鎵嶅湪琚彂閫併�傜敱浜庡巻鍙茬殑鍘熷洜锛岃〃鍗曚娇鐢ㄧ殑Url缂栫爜瀹炵幇骞朵笉绗﹀悎鏈�鏂扮殑鏍囧噯銆備緥濡傚浜庣┖鏍间娇鐢ㄧ殑缂栫爜骞朵笉鏄�%20锛岃�屾槸+鍙凤紝濡傛灉琛ㄥ崟浣跨敤鐨勬槸Post鏂规硶鎻愪氦鐨勶紝鎴戜滑鍙互鍦℉TTP澶翠腑鐪嬪埌鏈変竴涓狢ontent-Type鐨刪eader锛屽�间负application/x-www-form-urlencoded銆傚ぇ閮ㄥ垎搴旂敤绋嬪簭鍧囪兘澶勭悊杩欑闈炴爣鍑嗗疄鐜扮殑Url缂栫爜锛屼絾鏄湪瀹㈡埛绔疛avascript涓紝骞舵病鏈変竴涓嚱鏁拌兘澶熷皢+鍙疯В鐮佹垚绌烘牸锛屽彧鑳借嚜宸卞啓杞崲鍑芥暟銆傝繕鏈夛紝瀵逛簬闈濧SCII瀛楃锛屼娇鐢ㄧ殑缂栫爜瀛楃闆嗗彇鍐充簬褰撳墠鏂囨。浣跨敤鐨勫瓧绗﹂泦銆備緥濡傛垜浠湪Html澶撮儴鍔犱笂

杩欐牱娴忚鍣ㄥ氨浼氫娇鐢╣b2312鍘绘覆鏌撴鏂囨。锛堟敞鎰忥紝褰揌TML鏂囨。涓病鏈夎缃meta鏍囩锛屽垯娴忚鍣ㄤ細鏍规嵁褰撳墠鐢ㄦ埛鍠滃ソ鍘昏嚜鍔ㄩ�夋嫨瀛楃闆嗭紝鐢ㄦ埛涔熷彲浠ュ己鍒跺綋鍓嶇綉绔欎娇鐢ㄦ煇涓寚瀹氱殑瀛楃闆嗭級銆傚綋鎻愪氦琛ㄥ崟鏃讹紝Url缂栫爜浣跨敤鐨勫瓧绗﹂泦灏辨槸gb2312銆�

涔嬪墠鍦ㄤ娇鐢ˋptana锛堜负浠�涔堜笓鎸嘺ptana涓嬮潰浼氭彁鍒帮級閬囧埌涓�涓緢杩锋儜鐨勯棶棰橈紝灏辨槸鍦ㄤ娇鐢╡ncodeURI鐨勬椂鍊欙紝鍙戠幇瀹冪紪鐮佸緱鍒扮殑缁撴灉鍜屾垜鎯崇殑寰堜笉涓�鏍枫�備笅闈㈡槸鎴戠殑绀轰緥浠g爜锛�

[html]view plaincopy

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

document.write(encodeURI("涓枃"));

杩愯缁撴灉杈撳嚭%E6%B6%93%EE%85%9F%E6%9E%83銆傛樉鐒惰繖骞朵笉鏄娇鐢║TF-8瀛楃闆嗚繘琛孶rl缂栫爜寰楀埌鐨勭粨鏋滐紙鍦℅oogle涓婃悳绱�"涓枃"锛孶rl涓樉绀虹殑鏄�%E4%B8%AD%E6%96%87锛夈��

鎵�浠ユ垜褰撴椂灏卞緢璐ㄧ枒锛岄毦閬揺ncodeURI杩樿窡椤甸潰缂栫爜鏈夊叧锛屼絾鏄垜鍙戠幇锛屾甯告儏鍐典笅锛屽鏋滀綘浣跨敤gb2312杩涜Url缂栫爜涔熶笉浼氬緱鍒拌繖涓粨鏋滅殑鎵嶆槸銆傚悗鏉ョ粓浜庤鎴戝彂鐜帮紝鍘熸潵鏄〉闈㈡枃浠跺瓨鍌ㄤ娇鐢ㄧ殑瀛楃闆嗗拰Meta鏍囩涓寚瀹氱殑瀛楃闆嗕笉涓�鑷村鑷寸殑闂銆侫ptana鐨勭紪杈戝櫒榛樿鎯呭喌涓嬩娇鐢║TF-8瀛楃闆嗐�備篃灏辨槸璇磋繖涓枃浠跺疄闄呭瓨鍌ㄧ殑鏃跺�欎娇鐢ㄧ殑鏄疷TF-8瀛楃闆嗐�備絾鏄敱浜嶮eta鏍囩涓寚瀹氫簡gb2312锛岃繖涓椂鍊欙紝娴忚鍣ㄥ氨浼氭寜鐓b2312鍘昏В鏋愯繖涓枃妗o紝閭d箞鑷劧鍦�"涓枃"杩欎釜瀛楃涓茶繖閲屽氨浼氬嚭閿欙紝鍥犱负"涓枃"瀛楃涓茬敤UTF-8缂栫爜杩囧悗寰楀埌鐨勫瓧鑺傛槸0xE4 0xB8 0xAD 0xE6 0x96 0x87锛岃繖6涓瓧鑺傚張琚祻瑙堝櫒鎷縢b2312鍘昏В鐮侊紝閭d箞灏变細寰楀埌鍙﹀涓変釜姹夊瓧"娑擃厽鏋�"锛圙BK涓竴涓眽瀛楀崰涓や釜瀛楄妭锛夛紝杩欎笁涓眽瀛楀湪浼犲叆encodeURI鍑芥暟涔嬪悗寰楀埌鐨勭粨鏋滃氨鏄�%E6%B6%93%EE%85%9F%E6%9E%83銆傚洜姝わ紝encodeURI浣跨敤鐨勮繕鏄疷TF-8锛屽苟涓嶄細鍙楀埌椤甸潰瀛楃闆嗙殑褰卞搷銆�

瀵逛簬鍖呭惈涓枃鐨刄rl鐨勫鐞嗛棶棰橈紝涓嶅悓娴忚鍣ㄦ湁涓嶅悓鐨勮〃鐜般�備緥濡傚浜嶪E锛屽鏋滀綘鍕鹃�変簡楂樼骇璁剧疆"鎬绘槸浠TF-8鍙戦�乁rl"锛岄偅涔圲rl涓殑璺緞閮ㄥ垎鐨勪腑鏂囦細浣跨敤UTF-8杩涜Url缂栫爜涔嬪悗鍙戦�佺粰鏈嶅姟绔紝鑰屾煡璇㈠弬鏁颁腑鐨勪腑鏂囬儴鍒嗕娇鐢ㄧ郴缁熼粯璁ゅ瓧绗﹂泦杩涜Url缂栫爜銆備负浜嗕繚璇佹渶澶т簰鎿嶄綔鎬э紝寤鸿鎵�鏈夋斁鍒癠rl涓殑缁勪欢鍏ㄩ儴鏄惧紡鎸囧畾鏌愪釜瀛楃闆嗚繘琛孶rl缂栫爜锛岃�屼笉渚濊禆浜庢祻瑙堝櫒鐨勯粯璁ゅ疄鐜般��

鍙﹀锛屽緢澶欻TTP鐩戣宸ュ叿鎴栬�呮祻瑙堝櫒鍦板潃鏍忕瓑鍦ㄦ樉绀篣rl鐨勬椂鍊欎細鑷姩灏哢rl杩涜涓�娆¤В鐮侊紙浣跨敤UTF-8瀛楃闆嗭級锛岃繖灏辨槸涓轰粈涔堝綋浣犲湪Firefox涓闂瓽oogle鎼滅储涓枃鐨勬椂鍊欙紝鍦板潃鏍忔樉绀虹殑Url鍖呭惈涓枃鐨勭紭鏁呫�備絾瀹為檯涓婂彂閫佺粰鏈嶅姟绔殑鍘熷Url杩樻槸缁忚繃缂栫爜鐨勩�備綘鍙互鍦ㄥ湴鍧�鏍忎笂浣跨敤Javascript璁块棶location.href灏卞彲浠ョ湅鍑烘潵浜嗐�傚湪鐮旂┒Url缂栬В鐮佺殑鏃跺�欏崈涓囧埆琚繖浜涘亣璞$粰杩锋儜浜嗐��

浣滀负鍓嶇锛屾瘡鏃ヤ笌 URL 鎵撲氦閬撴槸蹇呬笉鍙皯鐨勩�備絾鏄篃璁告瘡澶╁彧鏄崟绾殑鐢紝瀵瑰叾鍙槸涓�鐭ュ崐瑙o紝闅忕潃宸ヤ綔鐨勫睍寮�锛屾垜鍙戠幇鍦ㄦ棩甯告姄鍖呰皟璇曪紝鎺ュ彛璋冪敤锛屾祻瑙堝櫒鍏煎绛夎澶氭柟闈紝涓嶆繁鍏ュ幓鐞嗚ВURL涓嶶RL缂栫爜鍒欎細韪╁埌寰堝鍧戙�傛晠鍐欎笅姝ょ瘒鏂囩珷锛岃瑙d竴涓� URL 銆�

URL 涓� URI

寰堝浜轰細娣锋穯杩欎袱涓悕璇嶃��

URL锛�(Uniform/Universal Resource Locator 鐨勭缉鍐欙紝缁熶竴璧勬簮瀹氫綅绗�)銆�

URI锛�(Uniform Resource Identifier 鐨勭缉鍐欙紝缁熶竴璧勬簮鏍囪瘑绗�)銆�

鍏崇郴锛�

URI 灞炰簬 URL 鏇翠綆灞傛鐨勬娊璞★紝涓�绉嶅瓧绗︿覆鏂囨湰鏍囧噯銆�

灏辨槸璇达紝URI 灞炰簬鐖剁被锛岃�� URL 灞炰簬 URI 鐨勫瓙绫汇�俇RL 鏄� URI 鐨勪竴涓瓙闆嗐��

浜岃�呯殑鍖哄埆鍦ㄤ簬锛孶RI 琛ㄧず璇锋眰鏈嶅姟鍣ㄧ殑璺緞锛屽畾涔夎繖涔堜竴涓祫婧愩�傝�� URL 鍚屾椂璇存槑瑕佸浣曡闂繖涓祫婧愶紙http://锛夈��

绔彛 涓� URL鏍囧噯鏍煎紡

浣曚负绔彛锛熺鍙�(Port)锛岀浉褰撲簬涓�绉嶆暟鎹殑浼犺緭閫氶亾銆傜敤浜庢帴鍙楁煇浜涙暟鎹紝鐒跺悗浼犺緭缁欑浉搴旂殑鏈嶅姟锛岃�岀數鑴戝皢杩欎簺鏁版嵁澶勭悊鍚庯紝鍐嶅皢鐩稿簲鐨勫洖澶嶉�氳繃寮�鍚殑绔彛浼犵粰瀵规柟銆�

绔彛鐨勪綔鐢細鍥犱负 IP 鍦板潃涓庣綉缁滄湇鍔$殑鍏崇郴鏄竴瀵瑰鐨勫叧绯汇�傛墍浠ュ疄闄呬笂鍥犵壒缃戜笂鏄�氳繃 IP 鍦板潃鍔犱笂绔彛鍙锋潵鍖哄垎涓嶅悓鐨勬湇鍔$殑銆�

绔彛鏄�氳繃绔彛鍙锋潵鏍囪鐨勶紝绔彛鍙峰彧鏈夋暣鏁帮紝鑼冨洿鏄粠0 鍒�65535銆�

URL 鏍囧噯鏍煎紡

閫氬父鑰岃█锛屾垜浠墍鐔熸倝鐨� URL 鐨勫父瑙佸畾涔夋牸寮忎负锛�

scheme://host[:port#]/path/.../[;url-params][?query-string][#anchor]

[plain]view plaincopy

scheme聽//鏈夋垜浠緢鐔熸倝鐨刪ttp銆乭ttps銆乫tp浠ュ強钁楀悕鐨別d2k锛岃繀闆风殑thunder绛夈��

host聽聽聽//HTTP鏈嶅姟鍣ㄧ殑IP鍦板潃鎴栬�呭煙鍚�

port#聽聽//HTTP鏈嶅姟鍣ㄧ殑榛樿绔彛鏄�80锛岃繖绉嶆儏鍐典笅绔彛鍙峰彲浠ョ渷鐣ャ�傚鏋滀娇鐢ㄤ簡鍒殑绔彛锛屽繀椤绘寚鏄庯紝渚嬪tomcat鐨勯粯璁ょ鍙f槸8080聽http://localhost:8080/

path聽聽聽//璁块棶璧勬簮鐨勮矾寰�

url-params聽聽//鎵�甯﹀弬鏁�

query-string聽聽聽聽//鍙戦�佺粰http鏈嶅姟鍣ㄧ殑鏁版嵁

anchor聽//閿氱偣瀹氫綅

鍒╃敤 鏍囩鑷姩瑙f瀽 url

寮�鍙戝綋涓竴涓緢甯歌鐨勫満鏅槸锛岄渶瑕佷粠 URL 涓彁鍙栦竴浜涢渶瑕佺殑鍏冪礌锛岃濡� host 銆� 璇锋眰鍙傛暟绛夌瓑銆�

閫氬父鐨勫仛娉曟槸鍐欐鍒欏幓鍖归厤鐩稿簲鐨勫瓧娈碉紝褰撶劧锛岃繖閲岃瀹夊埄涓嬭堪杩欑鏂规硶锛屾潵鑷�James鐨� blog锛屽師鐞嗘槸鍔ㄦ�佸垱寤轰竴涓� a 鏍囩锛屽埄鐢ㄦ祻瑙堝櫒鐨勪竴浜涘師鐢熸柟娉曞強涓�浜涙鍒欙紙涓轰簡鍋ュ.鎬ф鍒欒繕鏄鐨勶級锛屽畬缇庤В鏋� URL 锛岃幏鍙栨垜浠兂瑕佺殑浠绘剰涓�涓儴鍒嗐��

浠g爜濡備笅锛�

[javascript]view plaincopy

//聽This聽function聽creates聽a聽new聽anchor聽element聽and聽uses聽location

//聽properties聽(inherent)聽to聽get聽the聽desired聽URL聽data.聽Some聽String

//聽operations聽are聽used聽(to聽normalize聽results聽across聽browsers).

functionparseURL(url)聽{

vara聽=聽聽document.createElement('a');

a.href聽=聽url;

return{

source:聽url,

protocol:聽a.protocol.replace(':',''),

host:聽a.hostname,

port:聽a.port,

query:聽a.search,

params:聽(function(){

varret聽=聽{},

seg聽=聽a.search.replace(/^\?/,'').split('&'),

len聽=聽seg.length,聽i聽=聽0,聽s;

for(;i

if(!seg[i])聽{continue;聽}

s聽=聽seg[i].split('=');

ret[s[0]]聽=聽s[1];

}

returnret;

})(),

file:聽(a.pathname.match(/([^/?#]+)$/i)聽||聽[,''])[1],

hash:聽a.hash.replace('#',''),

path:聽a.pathname.replace(/^([^/])/,'/$1'),

relative:聽(a.href.match(/tps?:\/[^/]+(.+)/)聽||聽[,''])[1],

segments:聽a.pathname.replace(/^\//,'').split('/')

};

}

Usage 浣跨敤鏂规硶锛�

[plain]view plaincopy

var聽myURL聽=聽parseURL('http://abc.com:8080/dir/index.html?id=255&m=hello#top');

myURL.file;聽聽聽聽聽//聽=聽'index.html'

myURL.hash;聽聽聽聽聽//聽=聽'top'

myURL.host;聽聽聽聽聽//聽=聽'abc.com'

myURL.query;聽聽聽聽//聽=聽'?id=255&m=hello'

myURL.params;聽聽聽//聽=聽Object聽=聽{聽id:聽255,聽m:聽hello聽}

myURL.path;聽聽聽聽聽//聽=聽'/dir/index.html'

myURL.segments;聽//聽=聽Array聽=聽['dir',聽'index.html']

myURL.port;聽聽聽聽聽//聽=聽'8080'

myURL.protocol;聽//聽=聽'http'

myURL.source;聽聽聽//聽=聽'http://abc.com:8080/dir/index.html?id=255&m=hello#top'

鍒╃敤涓婅堪鏂规硶锛屽嵆鍙В鏋愬緱鍒� URL 鐨勪换鎰忛儴鍒嗐��

URL 缂栫爜

涓轰粈涔堣杩涜URL缂栫爜锛熼�氬父濡傛灉涓�鏍蜂笢瑗块渶瑕佺紪鐮侊紝璇存槑杩欐牱涓滆タ骞朵笉閫傚悎鐩存帴杩涜浼犺緭銆�

1銆佷細寮曡捣姝т箟锛氫緥濡� URL 鍙傛暟瀛楃涓蹭腑浣跨敤 key=value 杩欐牱鐨勯敭鍊煎褰㈠紡鏉ヤ紶鍙傦紝閿�煎涔嬮棿浠� & 绗﹀彿鍒嗛殧锛屽?postid=5038412&t=1450591802326锛屾湇鍔″櫒浼氭牴鎹弬鏁颁覆鐨� & 鍜� = 瀵瑰弬鏁拌繘琛岃В鏋愶紝濡傛灉 value 瀛楃涓蹭腑鍖呭惈浜� = 鎴栬�� & 锛屽瀹濇磥鍏徃鐨勭畝绉颁负P&G锛屽亣璁鹃渶瑕佸綋鍋氬弬鏁板幓浼犻�掞紝閭d箞鍙兘URL鎵�甯﹀弬鏁板彲鑳戒細鏄繖鏍�?name=P&G&t=1450591802326锛屽洜涓哄弬鏁颁腑澶氫簡涓�涓�&鍔垮繀浼氶�犳垚鎺ユ敹 URL 鐨勬湇鍔″櫒瑙f瀽閿欒锛屽洜姝ゅ繀椤诲皢寮曡捣姝т箟鐨� & 鍜� = 绗﹀彿杩涜杞箟锛� 涔熷氨鏄鍏惰繘琛岀紪鐮併��

2銆侀潪娉曞瓧绗︼細鍙堝锛孶RL 鐨勭紪鐮佹牸寮忛噰鐢ㄧ殑鏄� ASCII 鐮侊紝鑰屼笉鏄� Unicode锛岃繖涔熷氨鏄浣犱笉鑳藉湪 URL 涓寘鍚换浣曢潪 ASCII 瀛楃锛屼緥濡備腑鏂囥�傚惁鍒欏鏋滃鎴风娴忚鍣ㄥ拰鏈嶅姟绔祻瑙堝櫒鏀寔鐨勫瓧绗﹂泦涓嶅悓鐨勬儏鍐典笅锛屼腑鏂囧彲鑳戒細閫犳垚闂銆�

閭d箞濡備綍缂栫爜锛熷涓嬶細

escape 銆� encodeURI 銆乪ncodeURIComponent

escape()

棣栧厛鎯冲0鏄庣殑鏄紝W3C鎶婅繖涓嚱鏁板簾寮冧簡锛岃韩涓轰竴鍚嶅墠绔鏋滆繕鐢ㄨ繖涓嚱鏁版槸瑕佹墦鑴哥殑銆�

escape鍙槸瀵瑰瓧绗︿覆杩涜缂栫爜锛堣�屽叾浣欎袱绉嶆槸瀵筓RL杩涜缂栫爜锛夛紝涓嶶RL缂栫爜鏃犲叧銆傜紪鐮佷箣鍚庣殑鏁堟灉鏄互 %XX 鎴栬�� %uXXXX 杩欑褰㈠紡鍛堢幇鐨勩�傚畠涓嶄細瀵�ASCII瀛楃銆佹暟瀛椔犱互鍙� @ * / +杩涜缂栫爜銆�

鏍规嵁 MDN 鐨勮鏄庯紝escape 搴斿綋鎹㈢敤涓� encodeURI 鎴� encodeURIComponent锛泆nescape 搴斿綋鎹㈢敤涓� decodeURI 鎴� decodeURIComponent銆俥scape 搴旇閬垮厤浣跨敤銆備妇渚嬪涓嬶細

[javascript]view plaincopy

encodeURI('https://www.baidu.com/聽a聽b聽c')

//聽"https://www.baidu.com/%20a%20b%20c"

encodeURIComponent('https://www.baidu.com/聽a聽b聽c')

//聽"https%3A%2F%2Fwww.baidu.com%2F%20a%20b%20c"

//鑰屄爀scape聽浼氱紪鐮佹垚涓嬮潰杩欐牱锛宔ocode聽浜嗗啋鍙峰嵈娌÷爀ncode聽鏂滄潬锛屽崄鍒嗘�紓锛屾晠搴熷純涔�

escape('https://www.baidu.com/聽a聽b聽c')

//聽"https%3A//www.baidu.com/%20a%20b%20c"

encodeURI()

encodeURI() 鏄� Javascript 涓湡姝g敤鏉ュ URL 缂栫爜鐨勫嚱鏁般�傚畠鐫�鐪间簬瀵规暣涓猆RL杩涜缂栫爜銆�

[javascript]view plaincopy

encodeURI("http://www.cnblogs.com/season-huang/some聽other聽thing");

//"http://www.cnblogs.com/season-huang/some%20other%20thing";

缂栫爜鍚庡彉涓轰笂杩扮粨鏋滐紝鍙互鐪嬪埌绌烘牸琚紪鐮佹垚浜�%20锛岃��鏂滄潬 /锛屽啋鍙� :骞舵病鏈夎缂栫爜銆�

鏄殑锛屽畠鐢ㄤ簬瀵规暣涓� URL 鐩存帴缂栫爜锛屼笉浼氬ASCII瀛楁瘝 銆佹暟瀛� 銆伮爚 ! @ # $ & * ( ) = : / , ; ? + ' 杩涜缂栫爜銆�

[javascript]view plaincopy

encodeURI("~!@#$&*()=:/,;?+'")

//聽~!@#$&*()=:/,;?+'

encodeURIComponent()

鍢匡紝鏈夌殑鏃跺�欙紝鎴戜滑鐨� URL 闀胯繖鏍峰瓙锛岃姹傚弬鏁颁腑甯︿簡鍙︿竴涓� URL 锛�

[javascript]view plaincopy

varURL聽="http://www.a.com?foo=http://www.b.com?t=123&s=456";

鐩存帴杩涜 encodeURI 鏄剧劧鏄笉琛岀殑銆傚洜涓� encodeURI 涓嶄細瀵瑰啋鍙� : 鍙婃枩鏉� / 杩涜杞箟锛岄偅涔堝氨浼氬嚭鐜颁笂杩版墍璇寸殑鏈嶅姟鍣ㄦ帴鍙楀埌涔嬪悗瑙f瀽浼氭湁姝т箟銆�

[javascript]view plaincopy

encodeURI(URL)

//聽"http://www.a.com?foo=http://www.b.com?t=123&b=456"

杩欎釜鏃跺�欙紝灏辫鐢ㄥ埌聽encodeURIComponent() 銆傚畠鐨勪綔鐢ㄦ槸瀵� URL 涓殑鍙傛暟杩涜缂栫爜锛岃浣忔槸瀵瑰弬鏁帮紝鑰屼笉鏄鏁翠釜 URL 杩涜缂栫爜銆�

鍥犱负瀹冧粎浠呬笉瀵�ASCII瀛楁瘝銆佹暟瀛� ~ ! * ( ) '杩涜缂栫爜銆�

閿欒鐨勭敤娉曪細

[javascript]view plaincopy

varURL聽="http://www.a.com?foo=http://www.b.com?t=123&s=456";

encodeURIComponent(URL);

//聽"http%3A%2F%2Fwww.a.com%3Ffoo%3Dhttp%3A%2F%2Fwww.b.com%3Ft%3D123%26s%3D456"

//聽閿欒鐨勭敤娉曪紝鐪嬪埌绗竴涓爃ttp聽鐨勫啋鍙峰強鏂滄潬涔熻聽encode聽浜�

姝g‘鐨勭敤娉曪細encodeURIComponent() 鐫�鐪间簬瀵瑰崟涓殑鍙傛暟杩涜缂栫爜锛�

[javascript]view plaincopy

varparam聽="http://www.b.com?t=123&s=456";聽//聽瑕佽缂栫爜鐨勫弬鏁�

URL聽="http://www.a.com?foo="+encodeURIComponent(param);

//"http://www.a.com?foo=http%3A%2F%2Fwww.b.com%3Ft%3D123%26s%3D456"

鍒╃敤涓婅堪鐨勪娇鐢�鏍囩瑙f瀽 URL 浠ュ強鏍规嵁涓氬姟鍦烘櫙閰嶅悎 encodeURI() 涓� encodeURIComponent() 渚胯兘澶熷緢濂界殑澶勭悊 URL 鐨勭紪鐮侀棶棰樸��

搴旂敤鍦烘櫙鏈�甯歌鐨勪竴涓槸鎵嬪伐鎷兼帴 URL 鐨勬椂鍊欙紝瀵规瘡瀵� key-value 鐢� encodeURIComponent 杩涜杞箟锛屽啀杩涜浼犺緭銆�

ASCII鐮佽〃鍙互鐪嬫垚鐢变笁閮ㄥ垎缁勬垚:

绗竴閮ㄥ垎:闈炴墦鍗扮殑鎺у埗瀛楃銆�鐢�00H鍒�1FH鍏�32涓�,涓�鑸敤鏉ラ�氳鎴栦綔涓烘帶鍒朵箣鐢ㄣ�傛湁浜涘彲浠ユ樉绀哄湪灞忓箷涓�,鏈変簺鍒欎笉鑳芥樉绀�,浣嗚兘鐪嬪埌鍏舵晥鏋�(濡傛崲琛屻�侀��鏍�).濡備笅琛�:

URL鍘熺悊銆乁RL缂栫爜銆乁RL鐗规畩瀛楃_第1张图片

绗簩閮ㄥ垎锛氭墦鍗板瓧绗︺��鐢�20H鍒�7FH鍏�96涓�,杩�95涓瓧绗︽槸鐢ㄦ潵琛ㄧず闃挎媺浼暟瀛椼�佽嫳鏂囧瓧姣嶅ぇ灏忓啓鍜屼笅鍒掔嚎銆佹嫭鍙风瓑绗﹀彿,閮藉彲浠ユ樉绀哄湪灞忓箷涓�.濡備笅琛�:

URL鍘熺悊銆乁RL缂栫爜銆乁RL鐗规畩瀛楃_第2张图片

绗笁閮ㄥ垎锛氭墿灞�

ASCII 鎵撳嵃瀛楃銆傜敱80H鍒�0FFH鍏�128涓瓧绗�,涓�鑸О涓�"鎵╁厖瀛楃",杩�128涓墿鍏呭瓧绗︽槸鐢盜BM鍒跺畾鐨�,骞堕潪鏍囧噯鐨凙SCII鐮�.杩欎簺瀛楃鏄敤鏉ヨ〃绀烘绾裤�侀煶鏍囧拰鍏跺畠娆ф床闈炶嫳璇郴鐨勫瓧姣嶃��

URL鍘熺悊銆乁RL缂栫爜銆乁RL鐗规畩瀛楃_第3张图片

ASCII 琛� 1

URL鍘熺悊銆乁RL缂栫爜銆乁RL鐗规畩瀛楃_第4张图片

ASCII 琛� 2

URL鍘熺悊銆乁RL缂栫爜銆乁RL鐗规畩瀛楃_第5张图片

涓�銆侀棶棰樼殑鐢辨潵

闂锛氬綋url鍦板潃涓寘鍚�&銆�+銆�%绛夌壒娈婂瓧绗︼紙涓昏鏄紶閫掑弬鏁版椂锛屽弬鏁扮殑鍐呭涓寘鍚繖浜涘瓧绗︼級鏃讹紝鍦板潃鏃犳晥銆傛瘮濡俬ttp://10.190.0.0:108/doc/test+desc2.bmp锛岃嫢鏂囦欢鍚嶄腑鍑虹幇+/&绛夌壒娈婂瓧绗︼紝鍚庡彴鎶�404閿欒锛屽嵆web鏈嶅姟鍣ㄦ壘涓嶅埌椤甸潰鎴栬�呰祫婧愩��

-------------------------------------------------------------------------------------------------------------------------------------

URL灏辨槸缃戝潃锛屽彧瑕佷笂缃戯紝灏变竴瀹氫細鐢ㄥ埌銆�

URL鍘熺悊銆乁RL缂栫爜銆乁RL鐗规畩瀛楃_第6张图片

涓�鑸潵璇达紝URL鍙兘浣跨敤鑻辨枃瀛楁瘝銆侀樋鎷変集鏁板瓧鍜屾煇浜涙爣鐐圭鍙凤紝涓嶈兘浣跨敤鍏朵粬鏂囧瓧鍜岀鍙枫�傛瘮濡傦紝涓栫晫涓婃湁鑻辨枃瀛楁瘝鐨勭綉鍧�鈥渉ttp://www.abc.com鈥濓紝浣嗘槸娌℃湁甯岃厞瀛楁瘝鐨勭綉鍧�鈥渉ttp://www.a尾纬.com鈥濓紙璇讳綔闃垮皵娉�-璐濆-浼界帥.com锛夈�傝繖鏄洜涓虹綉缁滄爣鍑哛FC 1738鍋氫簡纭�ц瀹氾細

"...Only alphanumerics [0-9a-zA-Z], the special characters "$-_.+!*'()," [not including the quotes - ed], and reserved characters used for their reserved purposes may be used unencoded within a URL."

鈥滃彧鏈夊瓧姣嶅拰鏁板瓧[0-9a-zA-Z]銆佷竴浜涚壒娈婄鍙封��$-_.+!*'(),鈥漑涓嶅寘鎷弻寮曞彿]銆佷互鍙婃煇浜涗繚鐣欏瓧锛屾墠鍙互涓嶇粡杩囩紪鐮佺洿鎺ョ敤浜嶶RL銆傗��

杩欐剰鍛崇潃锛屽鏋淯RL涓湁姹夊瓧锛屽氨蹇呴』缂栫爜鍚庝娇鐢ㄣ�備絾鏄夯鐑︾殑鏄紝RFC 1738娌℃湁瑙勫畾鍏蜂綋鐨勭紪鐮佹柟娉曪紝鑰屾槸浜ょ粰搴旂敤绋嬪簭锛堟祻瑙堝櫒锛夎嚜宸卞喅瀹氥�傝繖瀵艰嚧鈥淯RL缂栫爜鈥濇垚涓轰簡涓�涓贩涔辩殑棰嗗煙銆�

鍦ㄤ娇鐢╱rl杩涜鍙傛暟浼犻�掓椂锛岀粡甯镐細浼犻�掍竴浜涗腑鏂囧悕锛堟垨鍚湁鐗规畩瀛楃锛夌殑鍙傛暟鎴朥RL鍦板潃锛屽湪鍚庡彴澶勭悊鏃朵細鍙戠敓杞崲閿欒銆傝繖浜涚壒娈婄鍙峰湪URL涓槸涓嶈兘鐩存帴浼犻�掔殑锛屽鏋滆鍦║RL涓紶閫掕繖浜涚壒娈婄鍙凤紝閭d箞灏辫浣跨敤浠栦滑鐨勭紪鐮佷簡銆傜紪鐮佺殑鏍煎紡涓猴細%鍔犲瓧绗︾殑ASCII鐮侊紝鍗充竴涓櫨鍒嗗彿%锛屽悗闈㈣窡瀵瑰簲瀛楃鐨凙SCII锛�16杩涘埗锛夌爜鍊笺�備緥濡傜┖鏍肩殑缂栫爜鍊兼槸"%20"銆備笅琛ㄤ腑鍒楀嚭浜嗕竴浜沀RL鐗规畩绗﹀彿鍙婄紪鐮併��

搴忓彿鐗规畩瀛楃鍚箟鍗佸叚杩涘埗鍊�

1.+URL 涓�+鍙疯〃绀虹┖鏍�%2B

2.绌烘牸URL涓殑绌烘牸鍙互鐢�+鍙锋垨鑰呯紪鐮�%20

3./鍒嗛殧鐩綍鍜屽瓙鐩綍%2F

4.?鍒嗛殧瀹為檯鐨� URL 鍜屽弬鏁�%3F

5.%鎸囧畾鐗规畩瀛楃%25

6.#琛ㄧず涔︾%23

7.&URL 涓寚瀹氱殑鍙傛暟闂寸殑鍒嗛殧绗�%26

8.=URL 涓寚瀹氬弬鏁扮殑鍊�%3D

渚嬶細瑕佷紶閫掑瓧绗︿覆鈥渢his%is#te=st&o k?+/鈥濅綔涓哄弬鏁皌浼犵粰te.asp锛屽垯URL鍙互鏄�:

te.asp?t=this%is#te=st&o k?+/ 鎴栬��

te.asp?t=this%is#te=st&o+k?+/ 锛堢┖鏍煎彲浠ョ敤 鎴�+浠f浛锛�

Java涓璘RL 鐨勭紪鐮佸拰瑙g爜鍑芥暟

java.NET.URLEncoder.encode(String s)鍜宩ava.Net.URLDecoder.decode(String s);

鍦╦avascript 涓璘RL 鐨勭紪鐮佸拰瑙g爜鍑芥暟

escape(String s)鍜�(String s) ;

濡傛灉浣跨敤escape()鍑芥暟锛屾眽瀛椾篃浼氳浆涓轰贡鐮併�傚悗鏉ュ氨鍐欎簡涓�娈礿s閲嶆柊瀹炵幇escape()鐨勫姛鑳斤紝杩欓噷鎷�#涓轰緥瀛愭潵璇存槑涓�涓嬶細锛堝叾浠栫鍙峰悓锛�

[javascript]view plaincopy

functionencodeValue(objValue)

{

if(objValue.indexOf("#")!=聽-1)

{

objValue=objValue.replace("#","#");

objValue=encodeValue(objValue);

}

returnobjValue;

}

鏈�鍚庤涓�鐐癸細鍦╱rl涓腑濡傛灉寰楅亣鍒�#浼氳嚜鍔ㄨ浆鎴�#锛岃繖鏍� request.getParameter("鍙傛暟")灏卞彲浠ュ緱鍒版纭殑缁撴灉銆�

涓嬮潰灏辫鎴戜滑鐪嬬湅锛屸�淯RL缂栫爜鈥濆埌搴曟湁澶氭贩涔便�傛垜浼氫緷娆″垎鏋愬洓绉嶄笉鍚岀殑鎯呭喌锛屽湪姣忎竴绉嶆儏鍐典腑锛屾祻瑙堝櫒鐨刄RL缂栫爜鏂规硶閮戒笉涓�鏍枫�傛妸瀹冧滑鐨勫樊寮傝В閲婃竻妤氫箣鍚庯紝鎴戝啀璇村浣曠敤JavaScript鎵惧埌涓�涓粺涓�鐨勭紪鐮佹柟娉曘��

浜屻�佹儏鍐�1锛氱綉鍧�璺緞涓寘鍚眽瀛�

鎵撳紑IE锛堟垜鐢ㄧ殑鏄�8.0鐗堬級锛岃緭鍏ョ綉鍧�鈥渉ttp://zh.wikipedia.org/wiki/鏄ヨ妭鈥濄�傛敞鎰忥紝鈥滄槬鑺傗�濊繖涓や釜瀛楁鏃舵槸缃戝潃璺緞鐨勪竴閮ㄥ垎銆�

鏌ョ湅HTTP璇锋眰鐨勫ご淇℃伅锛屼細鍙戠幇IE瀹為檯鏌ヨ鐨勭綉鍧�鏄�渉ttp://zh.wikipedia.org/wiki/%E6%98%A5%E8%8A%82鈥濄�備篃灏辨槸璇达紝IE鑷姩灏嗏�滄槬鑺傗�濈紪鐮佹垚浜嗏��%E6%98%A5%E8%8A%82鈥濄��

URL鍘熺悊銆乁RL缂栫爜銆乁RL鐗规畩瀛楃_第7张图片

鎴戜滑鐭ラ亾锛屸�滄槬鈥濆拰鈥滆妭鈥濈殑utf-8缂栫爜鍒嗗埆鏄�淓6 98 A5鈥濆拰鈥淓8 8A 82鈥濓紝鍥犳锛屸��%E6%98%A5%E8%8A%82鈥濆氨鏄寜鐓ч『搴忥紝鍦ㄦ瘡涓瓧鑺傚墠鍔犱笂%鑰屽緱鍒扮殑銆�

鍦‵irefox涓�娴嬭瘯锛屼篃寰楀埌浜嗗悓鏍风殑缁撴灉銆傛墍浠ワ紝缁撹1灏辨槸锛岀綉鍧�璺緞鐨勭紪鐮侊紝鐢ㄧ殑鏄痷tf-8缂栫爜銆�

涓夈�佹儏鍐�2锛氭煡璇㈠瓧绗︿覆鍖呭惈姹夊瓧

鍦↖E涓緭鍏ョ綉鍧�鈥渉ttp://www.baidu.com/s?wd=鏄ヨ妭鈥濄�傛敞鎰忥紝鈥滄槬鑺傗�濊繖涓や釜瀛楁鏃跺睘浜庢煡璇㈠瓧绗︿覆锛屼笉灞炰簬缃戝潃璺緞锛屼笉瑕佷笌鎯呭喌1娣锋穯銆�

鏌ョ湅HTTP璇锋眰鐨勫ご淇℃伅锛屼細鍙戠幇IE灏嗏�滄槬鑺傗�濊浆鍖栨垚浜嗕竴涓贡鐮併��

URL鍘熺悊銆乁RL缂栫爜銆乁RL鐗规畩瀛楃_第8张图片

鍒囨崲鍒板崄鍏繘鍒舵柟寮忥紝鎵嶈兘娓呮鍦扮湅鍒帮紝鈥滄槬鑺傗�濊杞垚浜嗏�淏4 BA BD DA鈥濄��

URL鍘熺悊銆乁RL缂栫爜銆乁RL鐗规畩瀛楃_第9张图片

鎴戜滑鐭ラ亾锛屸�滄槬鈥濆拰鈥滆妭鈥濈殑GB2312缂栫爜锛堟垜鐨�鎿嶄綔绯荤粺鈥淲indows XP鈥濅腑鏂囩増鐨勯粯璁ょ紪鐮侊級鍒嗗埆鏄�淏4 BA鈥濆拰鈥淏D DA鈥濄�傚洜姝わ紝IE瀹為檯涓婂氨鏄皢鏌ヨ瀛楃涓诧紝浠B2312缂栫爜鐨勬牸寮忓彂閫佸嚭鍘汇��

Firefox鐨勫鐞嗘柟娉曪紝鐣ユ湁涓嶅悓銆傚畠鍙戦�佺殑HTTP Head鏄�渨d=%B4%BA%BD%DA鈥濄�備篃灏辨槸璇达紝鍚屾牱閲囩敤GB2312缂栫爜锛屼絾鏄湪姣忎釜瀛楄妭鍓嶅姞涓婁簡%銆�

URL鍘熺悊銆乁RL缂栫爜銆乁RL鐗规畩瀛楃_第10张图片

鎵�浠ワ紝缁撹2灏辨槸锛屾煡璇㈠瓧绗︿覆鐨勭紪鐮侊紝鐢ㄧ殑鏄�鎿嶄綔绯荤粺鐨勯粯璁ょ紪鐮併��

鍥涖�佹儏鍐�3锛欸et鏂规硶鐢熸垚鐨刄RL鍖呭惈姹夊瓧

鍓嶉潰璇寸殑鏄洿鎺ヨ緭鍏ョ綉鍧�鐨勬儏鍐碉紝浣嗘槸鏇村父瑙佺殑鎯呭喌鏄紝鍦ㄥ凡鎵撳紑鐨勭綉椤典笂锛岀洿鎺ョ敤Get鎴朠ost鏂规硶鍙戝嚭HTTP璇锋眰銆�

鏍规嵁鍙版咕涓叴澶у鍚曠憺楹熻�佸笀鐨勮瘯楠�锛岃繖鏃剁殑缂栫爜鏂规硶鐢辩綉椤电殑缂栫爜鍐冲畾锛屼篃灏辨槸鐢盚TML婧愮爜涓瓧绗﹂泦鐨勮瀹氬喅瀹氥��

濡傛灉涓婇潰杩欎竴琛屾渶鍚庣殑charset鏄疷TF-8锛屽垯URL灏变互UTF-8缂栫爜锛涘鏋滄槸GB2312锛孶RL 灏变互GB2312缂栫爜銆�

涓句緥鏉ヨ锛岀櫨搴︽槸GB2312缂栫爜锛孏oogle鏄疷TF-8缂栫爜銆傚洜姝わ紝浠庡畠浠殑鎼滅储妗嗕腑鎼滅储鍚屼竴涓瘝鈥滄槬鑺傗�濓紝鐢熸垚鐨勬煡璇㈠瓧绗︿覆鏄笉涓�鏍风殑銆�

鐧惧害鐢熸垚鐨勬槸%B4%BA%BD%DA锛岃繖鏄疓B2312缂栫爜銆�

URL鍘熺悊銆乁RL缂栫爜銆乁RL鐗规畩瀛楃_第11张图片

Google鐢熸垚鐨勬槸%E6%98%A5%E8%8A%82锛岃繖鏄疷TF-8缂栫爜銆�

URL鍘熺悊銆乁RL缂栫爜銆乁RL鐗规畩瀛楃_第12张图片

鎵�浠ワ紝缁撹3灏辨槸锛孏ET鍜孭OST鏂规硶鐨勭紪鐮侊紝鐢ㄧ殑鏄綉椤电殑缂栫爜銆�

浜斻�佹儏鍐�4锛欰jax璋冪敤鐨刄RL鍖呭惈姹夊瓧

鍓嶉潰涓夌鎯呭喌閮芥槸鐢辨祻瑙堝櫒鍙戝嚭HTTP璇锋眰锛屾渶鍚庝竴绉嶆儏鍐靛垯鏄敱Javascript鐢熸垚HTTP璇锋眰锛屼篃灏辨槸Ajax璋冪敤銆傝繕鏄牴鎹悤鐟為簾鑰佸笀鐨勬枃绔狅紝鍦ㄨ繖绉嶆儏鍐典笅锛孖E鍜孎irefox鐨勫鐞嗘柟寮忓畬鍏ㄤ笉涓�鏍枫��

涓句緥鏉ヨ锛屾湁杩欐牱涓よ浠g爜锛�

url = url + "?q=" +document.myform.elements[0].value; // 鍋囧畾鐢ㄦ埛鍦ㄨ〃鍗曚腑鎻愪氦鐨勫�兼槸鈥滄槬鑺傗�濊繖涓や釜瀛�

http_request.open('GET', url, true);

閭d箞锛屾棤璁虹綉椤典娇鐢ㄤ粈涔堝瓧绗﹂泦锛孖E浼犻�佺粰鏈嶅姟鍣ㄧ殑鎬绘槸鈥渜=%B4%BA%BD%DA鈥濓紝鑰孎irefox浼犻�佺粰鏈嶅姟鍣ㄧ殑鎬绘槸鈥渜=%E6%98%A5%E8%8A%82鈥濄�備篃灏辨槸璇达紝鍦ˋjax璋冪敤涓紝IE鎬绘槸閲囩敤GB2312缂栫爜锛堟搷浣滅郴缁熺殑榛樿缂栫爜锛夛紝鑰孎irefox鎬绘槸閲囩敤utf-8缂栫爜銆傝繖灏辨槸鎴戜滑鐨勭粨璁�4銆�

鍏�丣avascript鍑芥暟锛歟scape()

濂戒簡锛屽埌姝や负姝紝鍥涚鎯呭喌閮借瀹屼簡銆�

鍋囧畾鍓嶉潰浣犻兘鐪嬫噦浜嗭紝閭d箞姝ゆ椂浣犲簲璇ヤ細鎰熷埌寰堝ご鐥涖�傚洜涓猴紝瀹炲湪澶贩涔变簡銆備笉鍚岀殑鎿嶄綔绯荤粺銆佷笉鍚岀殑娴忚鍣ㄣ�佷笉鍚岀殑缃戦〉瀛楃闆嗭紝灏嗗鑷村畬鍏ㄤ笉鍚岀殑缂栫爜缁撴灉銆傚鏋滅▼搴忓憳瑕佹妸姣忎竴绉嶇粨鏋滈兘鑰冭檻杩涘幓锛屾槸涓嶆槸澶亹鎬栦簡锛熸湁娌℃湁鍔炴硶锛岃兘澶熶繚璇佸鎴风鍙敤涓�绉嶇紪鐮佹柟娉曞悜鏈嶅姟鍣ㄥ彂鍑鸿姹傦紵

鍥炵瓟鏄湁鐨勶紝灏辨槸浣跨敤Javascript鍏堝URL缂栫爜锛岀劧鍚庡啀鍚戞湇鍔″櫒鎻愪氦锛屼笉瑕佺粰娴忚鍣ㄦ彃鎵嬬殑鏈轰細銆傚洜涓篔avascript鐨勮緭鍑烘�绘槸涓�鑷寸殑锛屾墍浠ュ氨淇濊瘉浜嗘湇鍔″櫒寰楀埌鐨勬暟鎹槸鏍煎紡缁熶竴鐨勩��

Javascript璇█鐢ㄤ簬缂栫爜鐨勫嚱鏁帮紝涓�鍏辨湁涓変釜锛屾渶鍙よ�佺殑涓�涓氨鏄痚scape()銆傝櫧鐒惰繖涓嚱鏁扮幇鍦ㄥ凡缁忎笉鎻愬�′娇鐢ㄤ簡锛屼絾鏄敱浜庡巻鍙插師鍥狅紝寰堝鍦版柟杩樺湪浣跨敤瀹冿紝鎵�浠ユ湁蹇呰鍏堜粠瀹冭璧枫��

瀹為檯涓婏紝escape()涓嶈兘鐩存帴鐢ㄤ簬URL缂栫爜锛屽畠鐨勭湡姝d綔鐢ㄦ槸杩斿洖涓�涓瓧绗︾殑Unicode缂栫爜鍊笺�傛瘮濡傗�滄槬鑺傗�濈殑杩斿洖缁撴灉鏄�%u6625%u8282锛屼篃灏辨槸璇村湪Unicode瀛楃闆嗕腑锛屸�滄槬鈥濇槸绗�6625涓紙鍗佸叚杩涘埗锛夊瓧绗︼紝鈥滆妭鈥濇槸绗�8282涓紙鍗佸叚杩涘埗锛夊瓧绗︺��

瀹冪殑鍏蜂綋瑙勫垯鏄紝闄や簡ASCII瀛楁瘝銆佹暟瀛椼�佹爣鐐圭鍙封�淍 * _ + - . /鈥濅互澶栵紝瀵瑰叾浠栨墍鏈夊瓧绗﹁繘琛岀紪鐮併�傚湪\u0000鍒癨u00ff涔嬮棿鐨勭鍙疯杞垚%xx鐨勫舰寮忥紝鍏朵綑绗﹀彿琚浆鎴�%uxxxx鐨勫舰寮忋�傚搴旂殑瑙g爜鍑芥暟鏄痷nescape()銆�

鎵�浠ワ紝鈥淗ello World鈥濈殑escape()缂栫爜灏辨槸鈥淗ello%20World鈥濄�傚洜涓虹┖鏍肩殑Unicode鍊兼槸20锛堝崄鍏繘鍒讹級銆�

杩樻湁涓や釜鍦版柟闇�瑕佹敞鎰忋��

棣栧厛锛屾棤璁虹綉椤电殑鍘熷缂栫爜鏄粈涔堬紝涓�鏃﹁Javascript缂栫爜锛屽氨閮藉彉涓簎nicode瀛楃銆備篃灏辨槸璇达紝Javascipt鍑芥暟鐨勮緭鍏ュ拰杈撳嚭锛岄粯璁ら兘鏄疷nicode瀛楃銆傝繖涓�鐐瑰涓嬮潰涓や釜鍑芥暟涔熼�傜敤銆�

URL鍘熺悊銆乁RL缂栫爜銆乁RL鐗规畩瀛楃_第13张图片

鍏舵锛宔scape()涓嶅鈥�+鈥濈紪鐮併�備絾鏄垜浠煡閬擄紝缃戦〉鍦ㄦ彁浜よ〃鍗曠殑鏃跺�欙紝濡傛灉鏈夌┖鏍硷紝鍒欎細琚浆鍖栦负+瀛楃銆傛湇鍔″櫒澶勭悊鏁版嵁鐨勬椂鍊欙紝浼氭妸+鍙峰鐞嗘垚绌烘牸銆傛墍浠ワ紝浣跨敤鐨勬椂鍊欒灏忓績銆�

涓冦�丣avascript鍑芥暟锛歟ncodeURI()

encodeURI()鏄疛avascript涓湡姝g敤鏉ュURL缂栫爜鐨勫嚱鏁般��

瀹冪潃鐪间簬瀵规暣涓猆RL杩涜缂栫爜锛屽洜姝ら櫎浜嗗父瑙佺殑绗﹀彿浠ュ锛屽鍏朵粬涓�浜涘湪缃戝潃涓湁鐗规畩鍚箟鐨勭鍙封��; / ? : @ & = + $ , #鈥濓紝涔熶笉杩涜缂栫爜銆傜紪鐮佸悗锛屽畠杈撳嚭绗﹀彿鐨剈tf-8褰㈠紡锛屽苟涓斿湪姣忎釜瀛楄妭鍓嶅姞涓�%銆�

URL鍘熺悊銆乁RL缂栫爜銆乁RL鐗规畩瀛楃_第14张图片

瀹冨搴旂殑瑙g爜鍑芥暟鏄痙ecodeURI()銆�

URL鍘熺悊銆乁RL缂栫爜銆乁RL鐗规畩瀛楃_第15张图片

闇�瑕佹敞鎰忕殑鏄紝瀹冧笉瀵瑰崟寮曞彿'缂栫爜銆�

鍏�丣avascript鍑芥暟锛歟ncodeURIComponent()

鏈�鍚庝竴涓狫avascript缂栫爜鍑芥暟鏄痚ncodeURIComponent()銆備笌encodeURI()鐨勫尯鍒槸锛屽畠鐢ㄤ簬瀵筓RL鐨勭粍鎴愰儴鍒嗚繘琛屼釜鍒紪鐮侊紝鑰屼笉鐢ㄤ簬瀵规暣涓猆RL杩涜缂栫爜銆�

鍥犳锛屸��; / ? : @ & = + $ , #鈥濓紝杩欎簺鍦╡ncodeURI()涓笉琚紪鐮佺殑绗﹀彿锛屽湪encodeURIComponent()涓粺缁熶細琚紪鐮併�傝嚦浜庡叿浣撶殑缂栫爜鏂规硶锛屼袱鑰呮槸涓�鏍枫��

URL鍘熺悊銆乁RL缂栫爜銆乁RL鐗规畩瀛楃_第16张图片

瀹冨搴旂殑瑙g爜鍑芥暟鏄痙ecodeURIComponent()銆�

你可能感兴趣的:(URL鍘熺悊銆乁RL缂栫爜銆乁RL鐗规畩瀛楃)