如何用 VB 或 VB.Net 來撰寫 ASP 的 Server.URLencode 函數

如何用 VB 或 VB.Net 來撰寫 ASP 的 Server.URLencode 函數

如何用 VB 或 VB.Net 來撰寫 ASP 的 Server.URLencode 編碼函數

大家都知道 ASP 本身的 Server 物件即提供 URLencode函數

但若想在 VB 或 VB.Net 中來進行URLencode的編碼ㄋ ??

請參考底下做法:

VB.Net 程式碼如下 :

Public Function URLEncode(ByRef strEnc As String) As String

Dim strTmp2, strChar, strTmp, strRet As String

Dim lngLoop As Integer

For lngLoop = 0 To strEnc.Length - 1

strChar = strEnc.Substring(lngLoop, 1)

Select Case Asc(strChar)

Case 48 To 57, 65 To 90, 97 To 122

strRet &= strChar

Case 32

strRet &= "+"

Case Else

strTmp = Hex(Asc(strChar))

If strTmp.Length > 4 Then strTmp = strTmp.Substring(4)

strRet &= "%" & strTmp.Substring(0, 2)

If strTmp.Length > 2 Then

strTmp2 = strTmp.Substring(2)

strRet &= IIf(IsNumeric(strTmp.Substring(2, 1)), Chr(Val("&H" & strTmp2)), "%" & strTmp2)

End If

End Select

Next

URLEncode = strRet

End Function

Public Function URLenc(ByVal strEnc As String) As String

Dim lngLoop, lngAsc As Long

Dim strChr As String

For lngLoop = 0 To strEnc.Length - 1

strChr = strEnc.Substring(lngLoop, 1)

If Math.Abs(Asc(strChr)) < 255 Then

URLenc &= strChr

Else

lngAsc = Asc(strChr)

If lngAsc < 0 Then lngAsc = lngAsc + 65536

URLenc &= "%" & Hex((lngAsc And -256) \ 255) & "%" & Hex(lngAsc And 255)

End If

Next

End Function

呼叫 :

MessageBox.Show(URLEncode("強力鎯頭 PowerHammer !"))

MessageBox.Show(URLenc("強力鎯頭 PowerHammer !"))

結果顯示 :

%B1j%A4O%EE%CF%C0Y+PowerHammer+%21

%B1%6A%A4%4F%EE%CF%C0%59 PowerHammer !

================================================================

VB6 程式碼如下 :

Public Function URLEncode(strEnc As String) As String

Dim strChar As String, strTmp As String, strTmp2 As String, strRet As String

Dim lngLoop As Long

For lngLoop = 1 To Len(strEnc)

strChar = Mid(strEnc, lngLoop, 1)

Select Case Asc(strChar)

Case 48 To 57, 65 To 90, 97 To 122

strRet = strRet & strChar

Case 32

strRet = strRet & "+"

Case Else

strTmp = Format(Hex(Asc(strChar)), "00")

strRet = strRet & "%" & Left(strTmp, 2)

strTmp2 = Mid(strTmp, 3, 2)

If Len(strTmp) > 3 Then

strRet = strRet & IIf(IsNumeric(Mid(strTmp, 3, 1)), Chr(Val("&H" & strTmp2)), "%" & strTmp2)

End If

End Select

Next

URLEncode = strRet

End Function

Public Function URLenc(strEnc As String) As String

Dim lngLoop As Long, lngAsc As Long

Dim strChr As String

For lngLoop = 1 To Len(strEnc)

strChr = Mid(strEnc, lngLoop, 1)

If Abs(Asc(strChr)) < 255 Then

URLenc = URLenc & strChr

Else

lngAsc = Asc(strChr)

If lngAsc < 0 Then lngAsc = lngAsc + 65536

URLenc = URLenc & "%" & Hex((lngAsc And -256) \ 255) & "%" & Hex(lngAsc And 255)

End If

Next

End Function

呼叫 :

MsgBox URLEncode("強力鎯頭 PowerHammer !")

MsgBox URLenc("強力鎯頭 PowerHammer !")

結果顯示 :

%B1j%A4O%EE%CF%C0Y+PowerHammer+%21

%B1%6A%A4%4F%EE%CF%C0%59 PowerHammer !

================================================================

ASP 程式碼如下(多此一舉,因為ASP本來就有Server.URLencode) :

Public Function URLEncode(strEnc)

Dim strChr, intAsc, strTmp, strTmp2, strRet, lngLoop

For lngLoop = 1 To Len(strEnc)

strChr = Mid(strEnc, lngLoop, 1)

intAsc = Asc(strChr)

If ((intAsc < 58) And (intAsc > 47)) Or ((intAsc < 91) And (intAsc > 64)) Or ((intAsc < 123) And (intAsc > 96)) Then

strRet = strRet & strChr

ElseIf intAsc = 32 Then

strRet = strRet & "+"

Else

strTmp = Hex(Asc(strChr))

strRet = strRet & "%" & Right("00" & Left(strTmp, 2), 2)

strTmp2 = Mid(strTmp, 3, 2)

If Len(strTmp) > 3 Then

If IsNumeric(Mid(strTmp, 3, 1)) Then

strRet = strRet & Chr(CInt("&H" & strTmp2))

Else

strRet = strRet & "%" & strTmp2

End If

End If

End If

Next

URLEncode = strRet

End Function

Public Function URLenc(strEnc)

Dim lngLoop, lngAsc, strChr

For lngLoop = 1 To Len(strEnc)

strChr = Mid(strEnc, lngLoop, 1)

If Abs(Asc(strChr)) < 255 Then

URLenc = URLenc & strChr

Else

lngAsc = Asc(strChr)

If lngAsc < 0 Then lngAsc = lngAsc + 65536

URLenc = URLenc & "%" & Hex((lngAsc And -256) \ 255) & "%" & Hex(lngAsc And 255)

End If

Next

End Function

呼叫 :

Response.write URLEncode("強力鎯頭 PowerHammer !")

Response.write URLenc("強力鎯頭 PowerHammer !")

結果顯示 :

%B1j%A4O%EE%CF%C0Y+PowerHammer+%21

%B1%6A%A4%4F%EE%CF%C0%59 PowerHammer !