如何 Encode 和 Decode URL 地址?

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 applebbs 的 blog

这个问题比较热门的说~asp 的 server 对象提供了一个 server.encode() 方法,但是却没有相应的 server.decode() 方法或者 unencode() 方法 >_< 也许 m$ 认为这没必要……大错特错啊。这样的方法非常有用,这里提供一种实现~包括 vbscript 和 jscript 代码。

vbscript

<% 
    function urldecode(str)
        str = replace(str, "+", " ")
        for i = 1 to len(str)
            st = mid(str, i, 1)
            if st = "%" then
                if i+2 < len(str) then
                    sr = sr & _
                        chr(clng("&h" & mid(str, i+1, 2)))
                    i = i+2
                end if
            else
                sr = sr & st
            end if
        next
        urldecode = sr
    end function

    function urlencode(str)
        urlencode = server.urlencode(str)
    end function

    str1 = "http://www.foo.com/blah.asp?foo=1 & 2 &g=0"
    str2 = urlencode(str1)
    str3 = urldecode(str2)
    response.write(str1 & "<br>" & str2 & "<br>" & str3)
%>
jscript
<script language=jscript runat=server> 

    function urldecode(str)
    {
        return unescape(str);
    }

    function urlencode(str)
    {
        str = escape(str);

        // jscript doesn't think '/' needs to be escaped...
        // i'm not sure it does either, but take it out to be
        // consistent with vbscript's built-in urlencode()

        while (str.indexof("/")!=-1)
        {
            str = str.replace("/","%2f");
        }
        return str;
    }

    var str1 = "http://www.foo.com/blah.asp?foo=1 & 2 &g=0";
    var str2 = urlencode(str1);
    var str3 = urldecode(str2);
    response.write(str1 + "<br>" + str2 + "<br>" + str3)
</script>
转自:http://dotnet.btobcn.net/article_detail.aspx?aid=75

本文关键:如何 Encode 和 Decode URL 地址?
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top