Chishin Blog 千里之堤毁于蚁穴 —— 小歆
VB URL编码函数
发表于 2014-4-14 | | 软件源码
VB UTF-8 URL编码函数:
  1. Public Function UTF8_URLEncoding(szInput)
  2.     Dim wch, uch, szRet
  3.     Dim x
  4.     Dim nAsc, nAsc2, nAsc3
  5.     If szInput = "" Then
  6.         UTF8_URLEncoding = szInput
  7.         Exit Function
  8.     End If
  9.     For x = 1 To Len(szInput)
  10.         wch = Mid(szInput, x, 1)
  11.         nAsc = AscW(wch)
  12.        
  13.         If nAsc < 0 Then nAsc = nAsc + 65536
  14.        
  15.         If (nAsc And &HFF80) = 0 Then
  16.             szRet = szRet & wch
  17.         Else
  18.             If (nAsc And &HF000) = 0 Then
  19.                 uch = "%" & Hex(((nAsc \ 2 ^ 6)) Or &HC0) & Hex(nAsc And &H3F Or &H80)
  20.                 szRet = szRet & uch
  21.             Else
  22.                 uch = "%" & Hex((nAsc \ 2 ^ 12) Or &HE0) & "%" & _
  23.                 Hex((nAsc \ 2 ^ 6) And &H3F Or &H80) & "%" & _
  24.                 Hex(nAsc And &H3F Or &H80)
  25.                 szRet = szRet & uch
  26.             End If
  27.         End If
  28.     Next
  29.     UTF8_URLEncoding = szRet
  30. End Function
VB GB2312 URL编码函数:
  1.    Public Function URLEncode(ByRef strURL)
  2.        Dim I
  3.        Dim tempStr
  4.        For I = 1 To Len(strURL)
  5.            If InStr("-,.0123456789/", Mid(strURL, I, 1)) Then
  6.                URLEncode = URLEncode & Mid(strURL, I, 1)
  7.            Else
  8.                If Asc(Mid(strURL, I, 1)) < 0 Then
  9.                    tempStr = "%" & Right(CStr(Hex(Asc(Mid(strURL, I, 1)))), 2)
  10.                    tempStr = "%" & Left(CStr(Hex(Asc(Mid(strURL, I, 1)))), Len(CStr(Hex(Asc(Mid(strURL, I, 1))))) - 2) & tempStr
  11.                    URLEncode = URLEncode & tempStr
  12.                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
  13.                    URLEncode = URLEncode & Mid(strURL, I, 1)
  14.                Else
  15.                    URLEncode = URLEncode & "%" & Hex(Asc(Mid(strURL, I, 1)))
  16.                End If
  17.            End If
  18.        Next
  19.    End Function

发表评论:

TOP