网页的url编码怎么用vb来实现?

比如百度,出来的结果是%B0%D9%B6%C8,怎么用vb代码实现
2025-06-22 09:06:04
推荐回答(1个)
回答1:

我发你编码和解码的vb实现代码:

Private Function URLEncode(ByVal strURL As String) As String
    Dim i As Long
    Dim tempStr As String
    For i = 1 To Len(strURL)
        If InStr("-,.0123456789", Mid(strURL, i, 1)) Then
            URLEncode = URLEncode & Mid(strURL, i, 1)
        Else
            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
        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