vbs url encode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
' -----------------------
' Function URLEncode
' 参考:
' http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_26732683.html
'
Function URLEncode(StringToEncode, UsePlusRatherThanHexForSpace)
  Dim TempAns, CurChr, iChar
  CurChr = 1
  Do Until CurChr - 1 = Len(StringToEncode)
    iChar = Asc(Mid(StringToEncode, CurChr, 1))
    If (iChar > 47 And iChar < 58)  Or (iChar > 64 And iChar < 91) Or (iChar > 96 And iChar < 123) Then
      TempAns = TempAns & Mid(StringToEncode, CurChr, 1)
    ElseIf iChar = 32 Then
      If UsePlusRatherThanHexForSpace Then
        TempAns = TempAns & "+"
      Else
        TempAns = TempAns & "%" & Hex(32)
      End If
    Else
      TempAns = TempAns & "%" & Right("00" & Hex(Asc(Mid(StringToEncode, CurChr, 1))), 2)
    End If
    CurChr = CurChr + 1
  Loop
  URLEncode = TempAns
End Function


' -----------------------
' Function URLEncode
' VBScript/ASP URLEncode function with charset (urlencode to utf-8 or other character encoding)    
' 参考:
' http://www.motobit.com/help/scptutl/sa323.htm
'
Function URLEncode(ByVal Data, CharSet)
  'Create a ByteArray object
  Dim ByteArray: Set ByteArray = CreateObject("ScriptUtils.ByteArray")
  If Len(CharSet)>0 Then ByteArray.CharSet = CharSet
    
  ByteArray.String = Data

  If ByteArray.Length > 0 Then
    Dim I, C, Out

    For I = 1 To ByteArray.Length
      'For each byte of the encoded data
      C = ByteArray(I)
      If C = 32 Then 'convert space to +
        Out = Out + "+"
      ElseIf (C < 48 Or c>126) Or (c>56 And c<=64) Then
        Out = Out + "%" + Hex(C)
      Else
        Out = Out + Chr(c)
      End If
    Next
    URLEncode = Out
  End If
End Function