VB,VBS,实现server.URLEncode及反编码功能的函数URLEncode()与URLDecode()
Function URLEncode(strURL) Dim I Dim tempStr For I = 1 To Len(strURL) If Asc(Mid(strURL, I, 1)) < 0 Then tempStr = "%" & Right(CStr(Hex(Asc(Mid(strURL, I, 1)))), 2) tempStr = "%" & Left(CStr(Hex(Asc(Mid(strURL, I, 1)))), Len(CStr(Hex(Asc(Mid(strURL, I, 1))))) - 2) & tempStr URLEncode = URLEncode & tempStr ElseIf (Asc(Mid(strURL, I, 1)) >= 65 And Asc(Mid(strURL, I, 1)) <= 90) Or (Asc(Mid(strURL, I, 1)) >= 97 And Asc(Mid(strURL, I, 1)) <= 122) Or (Asc(Mid(strURL, I, 1)) >= 48 And Asc(Mid(strURL, I, 1)) <= 57) Then URLEncode = URLEncode & Mid(strURL, I, 1) Else URLEncode = URLEncode & "%" & Hex(Asc(Mid(strURL, I, 1))) End If Next End Function Function URLDecode(strURL) Dim I If InStr(strURL, "%") = 0 Then URLDecode = strURL Exit Function End If For I = 1 To Len(strURL) If Mid(strURL, I, 1) = "%" Then If Eval("&H" & Mid(strURL, I + 1, 2)) > 127 Then URLDecode = URLDecode & Chr(Eval("&H" & Mid(strURL, I + 1, 2) & Mid(strURL, I + 4, 2))) I = I + 5 Else URLDecode = URLDecode & Chr(Eval("&H" & Mid(strURL, I + 1, 2))) I = I + 2 End If Else URLDecode = URLDecode & Mid(strURL, I, 1) End If Next End Function
1 <script language=vbs>
2 Function URLEncode(strURL)
3 Dim I
4 Dim tempStr
5 For I = 1 To Len(strURL)
6 If Asc(Mid(strURL, I, 1)) < 0 Then
7 tempStr = "%" & Right(CStr(Hex(Asc(Mid(strURL, I, 1)))), 2)
8 tempStr = "%" & Left(CStr(Hex(Asc(Mid(strURL, I, 1)))), Len(CStr(Hex(Asc(Mid(strURL, I, 1))))) - 2) & tempStr
9 URLEncode = URLEncode & tempStr
10 ElseIf (Asc(Mid(strURL, I, 1)) >= 65 And Asc(Mid(strURL, I, 1)) <= 90) Or (Asc(Mid(strURL, I, 1)) >= 97 And Asc(Mid(strURL, I, 1)) <= 122) Or (Asc(Mid(strURL, I, 1)) >= 48 And Asc(Mid(strURL, I, 1)) <= 57) Then
11 URLEncode = URLEncode & Mid(strURL, I, 1)
12 Else
13 URLEncode = URLEncode & "%" & Hex(Asc(Mid(strURL, I, 1)))
14 End If
15 Next
16 End Function
17
18 Function URLDecode(strURL)
19 Dim I
20
21 If InStr(strURL, "%") = 0 Then
22 URLDecode = strURL
23 Exit Function
24 End If
25
26 For I = 1 To Len(strURL)
27 If Mid(strURL, I, 1) = "%" Then
28 If Eval("&H" & Mid(strURL, I + 1, 2)) > 127 Then
29 URLDecode = URLDecode & Chr(Eval("&H" & Mid(strURL, I + 1, 2) & Mid(strURL, I + 4, 2)))
30 I = I + 5
31 Else
32 URLDecode = URLDecode & Chr(Eval("&H" & Mid(strURL, I + 1, 2)))
33 I = I + 2
34 End If
35 Else
36 URLDecode = URLDecode & Mid(strURL, I, 1)
37 End If
38 Next
39 End Function
40
41 msgbox URLEncode("我是谁呀haha"),,"URLEncode(""我是谁呀haha"")"
42 msgbox URLEncode("yongfa365"),,"URLEncode(""yongfa365"")"
43 msgbox URLEncode("How are you ?"),,"URLEncode(""How are you ?"")"
44 msgbox URLDecode(URLEncode("我是谁呀haha")),,"URLDecode(URLEncode(""我是谁呀haha""))"
45 window.close()
46 </script>
47
2 Function URLEncode(strURL)
3 Dim I
4 Dim tempStr
5 For I = 1 To Len(strURL)
6 If Asc(Mid(strURL, I, 1)) < 0 Then
7 tempStr = "%" & Right(CStr(Hex(Asc(Mid(strURL, I, 1)))), 2)
8 tempStr = "%" & Left(CStr(Hex(Asc(Mid(strURL, I, 1)))), Len(CStr(Hex(Asc(Mid(strURL, I, 1))))) - 2) & tempStr
9 URLEncode = URLEncode & tempStr
10 ElseIf (Asc(Mid(strURL, I, 1)) >= 65 And Asc(Mid(strURL, I, 1)) <= 90) Or (Asc(Mid(strURL, I, 1)) >= 97 And Asc(Mid(strURL, I, 1)) <= 122) Or (Asc(Mid(strURL, I, 1)) >= 48 And Asc(Mid(strURL, I, 1)) <= 57) Then
11 URLEncode = URLEncode & Mid(strURL, I, 1)
12 Else
13 URLEncode = URLEncode & "%" & Hex(Asc(Mid(strURL, I, 1)))
14 End If
15 Next
16 End Function
17
18 Function URLDecode(strURL)
19 Dim I
20
21 If InStr(strURL, "%") = 0 Then
22 URLDecode = strURL
23 Exit Function
24 End If
25
26 For I = 1 To Len(strURL)
27 If Mid(strURL, I, 1) = "%" Then
28 If Eval("&H" & Mid(strURL, I + 1, 2)) > 127 Then
29 URLDecode = URLDecode & Chr(Eval("&H" & Mid(strURL, I + 1, 2) & Mid(strURL, I + 4, 2)))
30 I = I + 5
31 Else
32 URLDecode = URLDecode & Chr(Eval("&H" & Mid(strURL, I + 1, 2)))
33 I = I + 2
34 End If
35 Else
36 URLDecode = URLDecode & Mid(strURL, I, 1)
37 End If
38 Next
39 End Function
40
41 msgbox URLEncode("我是谁呀haha"),,"URLEncode(""我是谁呀haha"")"
42 msgbox URLEncode("yongfa365"),,"URLEncode(""yongfa365"")"
43 msgbox URLEncode("How are you ?"),,"URLEncode(""How are you ?"")"
44 msgbox URLDecode(URLEncode("我是谁呀haha")),,"URLDecode(URLEncode(""我是谁呀haha""))"
45 window.close()
46 </script>
47