VBScript、VB.NET 中的 URLEncode、URLDecode

作者: 来源: 日期:2009-4-18

(本文的 URLEncode、URLDecode 是按 ANSI 编码的。ITPOW编辑注)

VBScript 中的 URLEncode、URLDecode

'Cooly(☆给我一个开心的理由!☆)
 
Public 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) Then
       URLEncode = URLEncode & Mid(strURL, I, 1)
    Else
       URLEncode = URLEncode & "%" & Hex(Asc(Mid(strURL, I, 1)))
    End If
Next
End Function
 
Public Function URLDecode(strURL)
Dim I
 
If InStr(strURL, "%") = 0 Then URLDecode = strURL: Exit Function
 
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

VBScript 中的 URLEncode、URLDecode

Private Sub Command1_Click()Sub Command1_Click()
Text2.Text = URLEncode(Text1.Text)
End Sub
 
 
Public Function URLEncode()Function URLEncode(ByRef strURL As String) As String
Dim I As Long
Dim tempStr As String
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) Then
       URLEncode = URLEncode & Mid(strURL, I, 1)
    Else
       URLEncode = URLEncode & "%" & Hex(Asc(Mid(strURL, I, 1)))
    End If
Next
End Function
 
Public Function URLDecode()Function URLDecode(ByRef strURL As String) As String
Dim I As Long
 
If InStr(strURL, "%") = 0 Then URLDecode = strURL: Exit Function
 
For I = 1 To Len(strURL)
    If Mid(strURL, I, 1) = "%" Then
       If Val("&H" & Mid(strURL, I + 1, 2)) > 127 Then
          URLDecode = URLDecode & Chr(Val("&H" & Mid(strURL, I + 1, 2) & Mid(strURL, I + 4, 2)))
          I = I + 5
       Else
          URLDecode = URLDecode & Chr(Val("&H" & Mid(strURL, I + 1, 2)))
          I = I + 2
       End If
    Else
       URLDecode = URLDecode & Mid(strURL, I, 1)
    End If
Next
End Function
 
Private Sub Command2_Click()Sub Command2_Click()
Text3.Text = URLDecode(Text2.Text)
End Sub
 
Private Sub Form_Load()Sub Form_Load()
Text1.Text = "http://www.microsoft.com/中国微软"
End Sub
相关文章