ASP 的 Server 对象提供了一个 Server.Encode() 方法,但是却没有相应的 Server.Decode() 方法或者 Unencode() 方法。也许 MS认为这没必要。其实这样的方法非常有用,这里提供一种实现~包括 VBScript 和 JScript 代码。
VBscript
1
<
%
2 Function URLDecode(str)
3 str = Replace (str, " + " , " " )
4 For i = 1 To Len (str)
5 sT = Mid (str, i, 1 )
6 If sT = " % " Then
7 If i + 2 < Len (str) Then
8 sR = sR & Chr ( CLng ( " &H " & Mid (str, i + 1 , 2 )))
9 i = i + 2
10 End If
11 Else
12 sR = sR & sT
13 End If
14 Next
15 URLDecode = sR
16 End Function
17
18 Function URLEncode(str)
19 URLEncode = Server.URLEncode(str)
20 End Function
21
22 str1 = " http://www.foo.com/blah.asp?foo=1 & 2 &g=0 "
23 str2 = URLEncode(str1)
24 str3 = URLDecode(str2)
25 Response.Write(str1 & " <br> " & str2 & " <br> " & str3)
26 % >
2 Function URLDecode(str)
3 str = Replace (str, " + " , " " )
4 For i = 1 To Len (str)
5 sT = Mid (str, i, 1 )
6 If sT = " % " Then
7 If i + 2 < Len (str) Then
8 sR = sR & Chr ( CLng ( " &H " & Mid (str, i + 1 , 2 )))
9 i = i + 2
10 End If
11 Else
12 sR = sR & sT
13 End If
14 Next
15 URLDecode = sR
16 End Function
17
18 Function URLEncode(str)
19 URLEncode = Server.URLEncode(str)
20 End Function
21
22 str1 = " http://www.foo.com/blah.asp?foo=1 & 2 &g=0 "
23 str2 = URLEncode(str1)
24 str3 = URLDecode(str2)
25 Response.Write(str1 & " <br> " & str2 & " <br> " & str3)
26 % >
JScript
1
<
script language
=
JScript runat
=
server
>
2 function URLDecode(str)
3 {
4 return unescape(str);
5 }
6
7 function URLEncode(str)
8 {
9 str = escape(str);
10 }
2 function URLDecode(str)
3 {
4 return unescape(str);
5 }
6
7 function URLEncode(str)
8 {
9 str = escape(str);
10 }