紧急问题:如何解码经过编码的URL?

譬如,在地址栏输入:http://localhost/中国

在404页面中捕获 Request.ServerVariables("QUERY_STRING")返回的是%E4%B8%AD%E5%9B%BD

到底怎样解码复原成“中国”?
---------------------------------------------------------------

有点难,帮你Up!
---------------------------------------------------------------

好像返回的应该是localhost%5C%D6%D0%B9%FA吧,
它是这样编码的,%5c=/ %d6%d0=中 %b9%fa=国

中=&HD0D6,在VBS中使用Chr(&HD0D6)就可以显示汉字“中”
国=&HFAB9,Chr(&HFAD9)

所以当第一个字节大于&H7F时,就可能是汉字,小于等&7F就是英文字符,比如上面的%5C=Chr(&H5C)。
---------------------------------------------------------------

 1<script language="vbs">   
 2var str="%E4%B8%AD%E5%9B%BD"   
 3alert URLDecode(str)   
 4function URLDecode(enStr)   
 5deStr=""   
 6strSpecial = "!""#$%&'()*+,/:;<=>?@[\\]^`{ ¦}~%"   
 7for i=1 to len(enStr)   
 8c=Mid(enStr,i,1)   
 9if c="%" then   
10v=eval("&h"+Mid(enStr,i+1,2))   
11if inStr(strSpecial,chr(v))>0 then   
12deStr=deStr&chr(v)   
13i=i+2   
14else   
15v=eval("&h"+Mid(enStr,i+1,2)+Mid(enStr,i+4,2))   
16deStr=deStr&chr(v)   
17i=i+5   
18end if   
19else   
20if c="+" then   
21deStr=deStr&" "   
22else   
23deStr=deStr&c   
24end if   
25end if   
26URLDecode=deStr   
27next   
28end function   
29</script>

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

我整理了一下:

 1<script language="vbscript">   
 2dim str   
 3str="%5C%D6%D0%B9%FA"   
 4alert URLDecode(str)   
 5function URLDecode(enStr)   
 6dim deStr,strSpecial   
 7dim c,i,v   
 8deStr=""   
 9strSpecial="!""#$%&'()*+,/:;<=>?@[\\]^`{ ¦}~%"   
10for i=1 to len(enStr)   
11c=Mid(enStr,i,1)   
12if c="%" then   
13v=eval("&h"+Mid(enStr,i+1,2))   
14if inStr(strSpecial,chr(v))>0 then   
15deStr=deStr&chr(v)   
16i=i+2   
17else   
18v=eval("&h"+Mid(enStr,i+1,2)+Mid(enStr,i+4,2))   
19deStr=deStr&chr(v)   
20i=i+5   
21end if   
22else   
23if c="+" then   
24deStr=deStr&" "   
25else   
26deStr=deStr&c   
27end if   
28end if   
29next   
30URLDecode=deStr   
31end function   
32</script>

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

恩,看了一下函数,可行!
收藏:)

Published At
Categories with Web编程
comments powered by Disqus