在 VBScript 中解码 URL 编码的 UTF-8 字符串 [英] Decoding URL encoded UTF-8 strings in VBScript

查看:25
本文介绍了在 VBScript 中解码 URL 编码的 UTF-8 字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对 VBScript 中的字符串进行 URL 解码.该字符串可能包含按照 UTF-8 编码为多个字节的 Unicode 字符.因此,例如Paris%20%E2%86%92%20Z%C3%BCrich"将解码为Paris → Zürich".

I need to URL decode a string in a VBScript. The string may contain Unicode characters which are encoded as multiple bytes as per UTF-8. So for example "Paris%20%E2%86%92%20Z%C3%BCrich" would decode to "Paris → Zürich".

为了完成这项工作,我使用了一段如下所示的代码:

To do the job, I'm using a piece of code that looks like this:

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 标准时代,必须有一种更简单的方法来完成这项工作,而无需大量手工制作的循环和条件.有什么建议吗?

This seems to be working well, but it looks exaggeratedly complex to me. In times of HTML5 and web standards, there must be a simpler way to get this done without a bunch of hand-made loops and conditions. Any suggestions?

推荐答案

我想展示适用于三种不同环境的三种方法.所有这些方法都需要 JScript 的 encodeURIComponentdecodeURIComponent 函数.

I want to show three methods for three vary environments. All of these methods requires JScript's encodeURIComponent and decodeURIComponent functions.

1.在 ASP 中,使用服务器端 JavaScript 是最合适的解决方案之一:

<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.在任何其他 WSH 中仅 32 位(由于 MSScriptControl.ScriptControl 是仅 32 位组件):

2. 32-Bit only (due to MSScriptControl.ScriptControl is 32-bit only component) in any other 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.在使用 WSC 的任何其他 WSH 中提供 64 位支持:

urlencdec.wsc(使用 WSC 向导创建)

<?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>

和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")

这篇关于在 VBScript 中解码 URL 编码的 UTF-8 字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆