我需要在VBScript中对字符串进行URL解码。该字符串可能包含作为UTF-8多字节编码的Unicode字符。因此例如,"Paris%20%E2%86%92%20Z%C3%BCrich"将解码为"Paris → Zürich"。
为了完成这个任务,我正在使用以下代码:
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
这似乎在工作得很好,但对我来说看起来过于复杂了。在 HTML5 和 Web 标准的时代,肯定有一种更简单的方法可以完成这个任务,而不需要大量手工循环和条件语句。有什么建议吗?
Request.Querystring()
会自动为您解码转义字符,但这仅在您通过查询字符串获取编码的字符串时才有帮助。 - MarthaRequest.Form
和Request.Cookies
。 - Kul-Tigin