2013-07-26 13 views
5

Potrzebuję adresu URL dekodować ciąg znaków w VBScript. Łańcuch może zawierać znaki Unicode, zakodowane jako wiele bajtów zgodnie z UTF-8. Na przykład "Paryż% 20% E2% 86% 92% 20Z% C3% BCrich" zostanie odkodowany na "Paryż → Zurych".Dekodowanie adresu URL zakodowanych ciągów UTF-8 w VBScript

Aby wykonać zadanie, Używam kawałek kodu, który wygląda tak:

Function URLDecode(str) 
    set list = CreateObject("System.Collections.ArrayList") 
    strLen = Len(str) 
    for i = 1 to strLen 
     sT = mid(str, i, 1) 
     if sT = "%" then 
      if i + 2 <= strLen then 
       list.Add cbyte("&H" & mid(str, i + 1, 2)) 
       i = i + 2 
      end if 
     else 
      list.Add asc(sT) 
     end if 
    next 
    depth = 0 
    for each by in list.ToArray() 
     if by and &h80 then 
      if (by and &h40) = 0 then 
       if depth = 0 then Err.Raise 5 
       val = val * 2^6 + (by and &h3f) 
       depth = depth - 1 
       if depth = 0 then 
        sR = sR & chrw(val) 
        val = 0 
       end if 
      elseif (by and &h20) = 0 then 
       if depth > 0 then Err.Raise 5 
       val = by and &h1f 
       depth = 1 
      elseif (by and &h10) = 0 then 
       if depth > 0 then Err.Raise 5 
       val = by and &h0f 
       depth = 2 
      else 
       Err.Raise 5 
      end if 
     else 
      if depth > 0 then Err.Raise 5 
      sR = sR & chrw(by) 
     end if 
    next 
    if depth > 0 then Err.Raise 5 
    URLDecode = sR 
End Function 

To wydaje się działać dobrze, ale wygląda na przesadnie skomplikowane do mnie. W czasach HTML5 i standardów internetowych, musi istnieć prostszy sposób na zrobienie tego bez użycia ręcznie robionych pętli i warunków. Jakieś sugestie?

Odpowiedz

15

Chcę pokazać trzy metody dla trzech różnych środowisk. Wszystkie te metody wymagają funkcji JScript's encodeURIComponent i decodeURIComponent.

1. ASP pomocą stronie serwera JavaScript jest jednym z najbardziej odpowiednich roztworów:

<script language="javascript" runat="server"> 
URL = { 
    encode : function(s){return encodeURIComponent(s).replace(/'/g,"%27").replace(/"/g,"%22")}, 
    decode : function(s){return decodeURIComponent(s.replace(/\+/g, " "))} 
} 
</script> 
<% 
Response.Write URL.decode("Paris%20%E2%86%92%20Z%C3%BCrich") 
Response.Write URL.encode("Paris → Zürich") 
%> 

2. tylko (32-bitowy powodu MSScriptControl.ScriptControl jest 32-bitowym składnikiem dopiero) w inny WSH:

Dim JSEngine 
Set JSEngine = CreateObject("MSScriptControl.ScriptControl") 
    JSEngine.Language = "JScript" 

Function UrlEncode(s) 
    UrlEncode = JSEngine.CodeObject.encodeURIComponent(s) 
    UrlEncode = Replace(UrlEncode, "'", "%27") 
    UrlEncode = Replace(UrlEncode, """", "%22") 
End Function 

Function UrlDecode(s) 
    UrlDecode = Replace(s, "+", " ") 
    UrlDecode = JSEngine.CodeObject.decodeURIComponent(UrlDecode) 
End Function 

WScript.Echo UrlDecode("Paris%20%E2%86%92%20Z%C3%BCrich") 
WScript.Echo UrlEncode("Paris → Zürich") 

3. 64-bitowych w inny WSH stosując WSC:

urlencdec.wsc (utworzone za pomocą WSC Wizard)

<?xml version="1.0"?> 
<component> 
<?component error="true" debug="true"?> 
    <registration 
     description="Url Encode/Decode Helper" 
     progid="JSEngine.Url" 
     version="1.0" 
     classid="{80246bcc-45d4-4e92-95dc-4fd9a93d8529}" 
    /> 
    <public> 
     <method name="encode"> 
      <PARAMETER name="s"/> 
     </method> 
     <method name="decode"> 
      <PARAMETER name="s"/> 
     </method> 
    </public> 
    <script language="JScript"> 
    <![CDATA[ 
     var description = new UrlEncodeDecodeHelper; 

     function UrlEncodeDecodeHelper() { 

      this.encode = encode; 
      this.decode = decode; 
     } 

     function encode(s) { 
      return encodeURIComponent(s).replace(/'/g,"%27").replace(/"/g,"%22"); 
     } 

     function decode(s) { 
      return decodeURIComponent(s.replace(/\+/g, " ")); 
     } 
    ]]> 
    </script> 
</component> 

i kod vbs:

Dim JSEngine 
Set JSEngine = GetObject("Script:C:\urlencdec.wsc") 

WScript.Echo JSEngine.decode("Paris%20%E2%86%92%20Z%C3%BCrich") 
WScript.Echo JSEngine.encode("Paris → Zürich") 
+2

To jest świetna odpowiedź, dzięki! –

+0

@ ft1 Nie ma za co. –

+0

@ ft1 Zrobiłem poprawkę, pamiętaj, że jeśli użyłeś drugiego rozwiązania. –

-1

Najlepszy sposób kodowania URL przy użyciu VBS polega na użyciu funkcji javascript encodeURIComponent() !! Komponent ScriptControl pozwala na uruchamianie kodu js ze środowiska VBS.

Oto moja funkcja urlencode, który działa dokładnie tak samo jak funkcja js (w rzeczywistości nazywa go !!):

 
Function URLEncode(str) 
    Dim encodedUrl 
    Set sc = CreateObject("MSScriptControl.ScriptControl") 
    sc.Language = "JScript" 
    sc.AddCode "var s = """ & str & """;" 
    sc.AddCode "function myEncode(s){return encodeURIComponent(s);}" 
    urlCodificada = sc.Eval("myEncode(s);") 
    Set sc = Nothing 
    URLEncode = encodedUrl 
End Function 
2

Czyste vbs klasyczny asp od urldecode funkcji, z UTF-8 wsparcie.

<% 
Function RegExTest(str,patrn) 
    Dim regEx 
    Set regEx = New RegExp 
    regEx.IgnoreCase = True 
    regEx.Pattern = patrn 
    RegExTest = regEx.Test(str) 
End Function 

Function URLDecode(sStr) 
    Dim str,code,a0 
    str="" 
    code=sStr 
    code=Replace(code,"+"," ") 
    While len(code)>0 
     If InStr(code,"%")>0 Then 
      str = str & Mid(code,1,InStr(code,"%")-1) 
      code = Mid(code,InStr(code,"%")) 
      a0 = UCase(Mid(code,2,1)) 
      If a0="U" And RegExTest(code,"^%u[0-9A-F]{4}") Then 
       str = str & ChrW((Int("&H" & Mid(code,3,4)))) 
       code = Mid(code,7) 
      ElseIf a0="E" And RegExTest(code,"^(%[0-9A-F]{2}){3}") Then 
       str = str & ChrW((Int("&H" & Mid(code,2,2)) And 15) * 4096 + (Int("&H" & Mid(code,5,2)) And 63) * 64 + (Int("&H" & Mid(code,8,2)) And 63)) 
       code = Mid(code,10) 
      ElseIf a0>="C" And a0<="D" And RegExTest(code,"^(%[0-9A-F]{2}){2}") Then 
       str = str & ChrW((Int("&H" & Mid(code,2,2)) And 3) * 64 + (Int("&H" & Mid(code,5,2)) And 63)) 
       code = Mid(code,7) 
      ElseIf (a0<="B" Or a0="F") And RegExTest(code,"^%[0-9A-F]{2}") Then 
       str = str & Chr(Int("&H" & Mid(code,2,2))) 
       code = Mid(code,4) 
      Else 
       str = str & "%" 
       code = Mid(code,2) 
      End If 
     Else 
      str = str & code 
      code = "" 
     End If 
    Wend 
    URLDecode = str 
End Function 


Response.Write URLDecode("Paris%20%E2%86%92%20Z%C3%BCrich") 'Paris → Zürich 
%> 
Powiązane problemy