在VBScript中解码URL编码的UTF-8字符串
我需要对VBScript中的字符串进行URL解码。字符串可能包含Unicode字符,这些字符按照UTF-8编码为多个字节。例如,“巴黎20%E2%86%92%20Z%C3%BCrich”将解码为“巴黎”→ 苏黎世” 为了完成这项工作,我使用了如下代码:在VBScript中解码URL编码的UTF-8字符串,utf-8,vbscript,urldecode,Utf 8,Vbscript,Urldecode,我需要对VBScript中的字符串进行URL解码。字符串可能包含Unicode字符,这些字符按照UTF-8编码为多个字节。例如,“巴黎20%E2%86%92%20Z%C3%BCrich”将解码为“巴黎”→ 苏黎世” 为了完成这项工作,我使用了如下代码: Function URLDecode(str) set list = CreateObject("System.Collections.ArrayList") strLen = Len(str) for i = 1 to
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
函数URLDecode(str)
set list=CreateObject(“System.Collections.ArrayList”)
strLen=Len(str)
对于i=1到strLen
sT=mid(str,i,1)
如果sT=“%”,则
如果i+2 0,则会出错。升高5
val=通过和&h1f
深度=1
elseif(by and&h10)=0然后
如果深度>0,则错误提升5
val=通过和&h0f
深度=2
其他的
呃,提高5分
如果结束
其他的
如果深度>0,则错误提升5
sR=sR&chrw(由)
如果结束
下一个
如果深度>0,则错误提升5
URLDecode=sR
端函数
这似乎很有效,但在我看来过于复杂了。在HTML5和web标准时代,必须有一种更简单的方法来实现这一点,而不需要大量手工制作的循环和条件。有什么建议吗?我想为三种不同的环境展示三种方法。所有这些方法都需要JScript的
encodeURIComponent
和decodeURIComponent
函数
1。在ASP中,使用服务器端JavaScript是最合适的解决方案之一:
URL={
encode:函数{返回encodeURIComponent.replace(/“/g,”%27”).replace(/“/g,”%22”),
decode:函数{返回decodeURIComponent(s.replace(/\+/g,”)}
}
2.32位(由于MSScriptControl.ScriptControl是仅32位的组件),在任何其他WSH中:Dim JSEngine
设置JSEngine=CreateObject(“MSScriptControl.ScriptControl”)
JSEngine.Language=“JScript”
函数UrlEncode(个)
UrlEncode=JSEngine.CodeObject.encodeURIComponent
UrlEncode=Replace(UrlEncode,“”,“%27”)
UrlEncode=Replace(UrlEncode,“”,“%22”)
端函数
函数UrlDecode(s)
UrlDecode=Replace(s,“+”,“”)
UrlDecode=JSEngine.CodeObject.decodeURIComponent(UrlDecode)
端函数
WScript.Echo UrlDecode(“巴黎%20%E2%86%92%20Z%C3%BCrich”)
Echo UrlEncode(“巴黎→ 苏黎世)
3.使用WSC在任何其他WSH中支持64位:
urlencdec.wsc(使用创建)
使用VBS编码URL的最佳方法是使用encodeURIComponent()javascript函数!!ScriptControl组件允许您从VBS环境运行js代码
下面是我的URLEncode函数,它的工作原理与js函数完全相同(实际上,它调用它!!):
函数URLEncode(str)
迪姆杜尔
Set sc=CreateObject(“MSScriptControl.ScriptControl”)
sc.Language=“JScript”
sc.AddCode“var s=”“”&str&“”;"
sc.AddCode“函数myEncode{return encodeURIComponent;}”
encodedUrl=sc.Eval(“myEncode;”)
设置sc=无
URLEncode=encodedUrl
端函数
纯vbs经典asp的URLDecode功能,支持utf-8
<%
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
%>
0
如果仪表(代码“%”)大于0,则
str=str&Mid(代码,1,仪表(代码,“%”-1)
代码=中间(代码,仪表(代码,“%”)
a0=UCase(Mid(代码,2,1))
如果a0=“U”和RegExTest(代码“^%U[0-9A-F]{4}”),则
str=str&ChrW((Int(&H)和Mid(代码,3,4)))
代码=Mid(代码,7)
ElseIf a0=“E”和RegExTest(代码,“^(%[0-9A-F]{2}{3}”),然后
str=str&ChrW((整数(“&H”&Mid(代码,2,2))和15)*4096+(整数(“&H”&Mid(代码,5,2))和63)*64+(整数(“&H”&Mid(代码,8,2))和63))
代码=Mid(代码,10)
ElseIf a0>=“C”和a0此vbscript代码受@kul Tigin解决方案的启发,以便在应用程序数据文件夹中生成urlencdec.wsc
,并将其与相同的vbscript文件一起使用:
'Question : Decoding URL encoded UTF-8 strings in VBScript
'URL : https://stackoverflow.com/questions/17880395/decoding-url-encoded-utf-8-strings-in-vbscript?answertab=active#tab-top
Option Explicit
Dim JSEngine,ws,WSC
Set ws = CreateObject("WScript.Shell")
WSC = ws.ExpandEnvironmentStrings("%AppData%\urlencdec.wsc")
Call Create_URL_ENC_DEC_Component(WSC)
Set JSEngine = GetObject("Script:"& WSC)
WScript.Echo JSEngine.decode("%D9%81%D9%8A%D9%84%D9%85-21Bridges-2019-%D9%85%D8%AA%D8%B1%D8%AC%D9%85")
WScript.Echo JSEngine.encode("Paris → Zürich")
Sub Create_URL_ENC_DEC_Component(WSC)
Dim fso,File
Set fso = CreateObject("Scripting.FileSystemObject")
Set File = fso.OpenTextFile(WSC,2,True)
File.WriteLine "<?xml version=""1.0""?>"
File.WriteLine "<component>"
File.WriteLine "<?component error=""true"" debug=""true""?>"
File.WriteLine "<registration"
File.WriteLine "description=""Url Encode / Decode Helper"""
File.WriteLine "progid=""JSEngine.Url"""
File.WriteLine "version=""1.0"""
File.WriteLine "classid=""{80246bcc-45d4-4e92-95dc-4fd9a93d8529}"""
File.WriteLine "/>"
File.WriteLine "<public>"
File.WriteLine "<method name=""encode"">"
File.WriteLine "<PARAMETER name=""s""/>"
File.WriteLine "</method>"
File.WriteLine "<method name=""decode"">"
File.WriteLine "<PARAMETER name=""s""/>"
File.WriteLine "</method>"
File.WriteLine "</public>"
File.WriteLine "<script language=""JScript"">"
File.WriteLine "<![CDATA["
File.WriteLine "var description = new UrlEncodeDecodeHelper;"
File.WriteLine "function UrlEncodeDecodeHelper() {"
File.WriteLine "this.encode = encode;"
File.WriteLine "this.decode = decode;"
File.WriteLine "}"
File.WriteLine "function encode(s) {"
File.WriteLine "return encodeURIComponent(s).replace(/'/g,""%27"").replace(/""/g,""%22"");"
File.WriteLine "}"
File.WriteLine "function decode(s) {"
File.WriteLine "return decodeURIComponent(s.replace(/\+/g, "" ""));"
File.WriteLine "}"
File.WriteLine "]]>"
File.WriteLine "</script>"
File.WriteLine "</component>"
End Sub
“问题:在VBScript中解码URL编码的UTF-8字符串
'网址:https://stackoverflow.com/questions/17880395/decoding-url-encoded-utf-8-strings-in-vbscript?answertab=active#tab-顶
选项显式
Dim JSEngine、ws、WSC
设置ws=CreateObject(“WScript.Shell”)
WSC=ws.ExpandEnvironmentStrings(“%AppData%\urlencdec.WSC”)
调用创建URL ENC DEC组件(WSC)
设置JSEngine=GetObject(“脚本:”&WSC)
WScript.Echo JSEngine.decode(“%D9%81%D9%8A%D9%84%D9%85-21Bridges-2019-%D9%85%D8%AA%D8%B1%D8%AC%D9%85”)
Echo JSEngine.encode(“Paris→ 苏黎世)
子创建URL ENC DEC组件(WSC)
Dim fso,文件
设置fso=CreateObject(“Scripting.FileSystemObject”)
Set File=fso.OpenTextFile(WSC,2,True)
File.WriteLine“”
File.WriteLine“”
File.WriteLine“”
File.WriteLine“”
File.WriteLine“”
File.WriteLine“”
File.WriteLine“”
File.WriteLine“”
File.WriteLine“”
File.WriteLine“”
File.WriteLine“”
File.WriteLine“”
File.WriteLine“”
File.WriteLine“我的代码(不创建临时文件)
编码URI
Option Explicit
Const WshRunning = 0,WshFailed = 1:Dim cmd,text,arr,i
If WScript.Arguments.Count()=0 Then
text=CreateObject("HTMLFile").parentWindow.clipboardData.GetData("text")
Else
ReDim arr(WScript.Arguments.Count-1)
For i=0 To WScript.Arguments.Count-1:arr(i)=WScript.Arguments(i):Next
text=Join(arr)
End if
if IsNull(text) Then
WScript.Echo "No data to execute.."
else
text=Replace(text,"""","\%22")
text=Replace(text,"'","\%27")
cmd="for /f ""usebackq"" %i in " & _
"(`mshta ""javascript:Code(close(new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(" & _
"encodeURIComponent('" & text & "')" & _
")));""`) do set e=%i&set e=!e:'=%27!&set e=!e:(=%28!&set e=!e:)=%29!&echo !e!"
Dim shell : Set shell = CreateObject("WScript.Shell")
Dim exec : Set exec = shell.Exec("cmd /v /c " & cmd)
While exec.Status = WshRunning
WScript.Sleep 50
Wend
Dim output
Dim err
If exec.ExitCode = WshFailed Then
err = exec.StdErr.ReadAll
Else
output = Split(exec.StdOut.ReadAll,Chr(10))
End If
If err="" Then
WScript.Echo output(2)
Else
WScript.Echo "Error=" & err
End If
End if
解码URI
Option Explicit
Dim Kod
If WScript.Arguments.Count()=0 Then
Kod=CreateObject("HTMLFile").parentWindow.clipboardData.GetData("text")
Else
Kod=WScript.Arguments(0)
End if
if IsNull(Kod) Then
WScript.Echo "No data to execute.."
Else
Dim chunk,Recoded,k1,k2,k3,i:i=0:Dim arr:arr=Split(Kod,"%")
Do While i <= UBound(arr)
if i<>0 Then
chunk = Left(arr(i),2)
If "&H"&Left(chunk,2)>=128 then
arr(i)="":i=i+1:chunk = chunk & Left(arr(i),2)
If "&H"&Left(chunk,2)<224 then
k1=Cint("&H"&Left(chunk,2)) mod 32
k2 = Cint("&H"&Mid(chunk,3,2)) mod 64
Recoded=ChrW( k2 + k1 * 64 )
Else
arr(i)="":i=i+1:chunk = chunk & Left(arr(i),4)
k1=Cint("&H"&Left(chunk,2)) mod 16
k2 = Cint("&H"&Mid(chunk,3,2)) mod 32
k3 = Cint("&H"&Mid(chunk,5,2)) mod 64
Recoded=ChrW( k3 + ( k2 + k1 * 64 ) * 64 )
End if
Else Recoded=Chr("&H"&chunk)
End If
arr(i)=Recoded & Mid(arr(i),3)
end if:i=i+1
loop
Kod=Join(arr,""):WScript.Echo Kod
End if
选项显式
暗柯达
如果WScript.Arguments.Count()=0,则
Kod=CreateObject(“HTMLFile”).parentWindow.clipboardData.GetData(“文本”)
其他的
Kod=WScript.Arguments(0)
如果结束
如果为空(Kod),则
Echo“没有要执行的数据…”
其他的
尺寸块,重新编码,k1,k2,k3,i:i=0:Dim-arr:arr=Split(Kod,“%”)
当i=128时,请执行此操作
arr(i)=“”:i=i+1:chunk=chunk&Left(arr(i),2)
如果“&H”&Left(chunk,2)@ft1我做了一个修复,请记住,如果您使用第二个解决方案。哇,不知道您可以像那样混合JavaScript和VB。头脑=崩溃。这打开了一个全新的世界时,陷入了经典的ASP!请注意,Request.Querystring()
将自动
Option Explicit
Dim Kod
If WScript.Arguments.Count()=0 Then
Kod=CreateObject("HTMLFile").parentWindow.clipboardData.GetData("text")
Else
Kod=WScript.Arguments(0)
End if
if IsNull(Kod) Then
WScript.Echo "No data to execute.."
Else
Dim chunk,Recoded,k1,k2,k3,i:i=0:Dim arr:arr=Split(Kod,"%")
Do While i <= UBound(arr)
if i<>0 Then
chunk = Left(arr(i),2)
If "&H"&Left(chunk,2)>=128 then
arr(i)="":i=i+1:chunk = chunk & Left(arr(i),2)
If "&H"&Left(chunk,2)<224 then
k1=Cint("&H"&Left(chunk,2)) mod 32
k2 = Cint("&H"&Mid(chunk,3,2)) mod 64
Recoded=ChrW( k2 + k1 * 64 )
Else
arr(i)="":i=i+1:chunk = chunk & Left(arr(i),4)
k1=Cint("&H"&Left(chunk,2)) mod 16
k2 = Cint("&H"&Mid(chunk,3,2)) mod 32
k3 = Cint("&H"&Mid(chunk,5,2)) mod 64
Recoded=ChrW( k3 + ( k2 + k1 * 64 ) * 64 )
End if
Else Recoded=Chr("&H"&chunk)
End If
arr(i)=Recoded & Mid(arr(i),3)
end if:i=i+1
loop
Kod=Join(arr,""):WScript.Echo Kod
End if