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

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

问题描述

我需要对VBScript中的字符串进行URL解码.该字符串可能包含Unicode字符,按照UTF-8编码为多个字节.因此,例如"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天全站免登陆