Sitemap Rss
·您当前位置:网站首页 >> ASP技术 >> 阅读文章

网址URL代码(urlcode)用ASP编码与解码的方法

来源:逍遥峡谷 发布时间:2011-05-05 浏览0条评论
内容简介

网址的URL里经常用到urlcode,其实严格意义上来说是urlencode将字符串以URL编码,这样可以用于非中文的浏览器上可以正常访问。比如用百度搜索中文内容之后会地址栏的地址会变成:http://www.baidu.com/s?wd=%E5%D0%D2%A3%CF%BF%B9%C8,其中后面的代码就是逍遥峡谷经过urlencode之后的代码。

相关关键词:urlcode   urldecode   urlencode   编码   解码  

本文链接:http://www.icoa.cn/a/482.html [复制网址]

我们用百度搜索中文的时候经常会看到这样的地址:http://www.baidu.com/s?wd=%E5%D0%D2%A3%CF%BF%B9%C8,其中后面的%E5%D0%D2%A3%CF%BF%B9%C8这一串复杂的代码就是用urlencode将中文字符串转换成URL编码之后的内容。

在ASP中有内置函数就是Server.URLEncode(),比如Server.URLEncode("逍遥峡谷")得到的就是上面这串代码,这有什么好处呢,就是可以在不同语言访问网址的时候可以正常访问。避免由于语言不同或者编码不同而造成无法访问的情况。

那么怎么把%E5%D0%D2%A3%CF%BF%B9%C8这一串复杂的代码解码为字符串呢?ASP里是没有这个函数的。

于是从网上搜到了两段自定义函数的代码,经过测试都可以使用。

第一段,使用的时候直接URLDecode("%E5%D0%D2%A3%CF%BF%B9%C8")就可以转换成中文字符串。
<%
Function URLDecode(ByVal urlcode)'URL反编码函数
    Dim start,final,length,char,i,butf8,pass
    Dim leftstr,rightstr,finalstr
    Dim b0,b1,bx,blength,position,u,utf8
    On Error Resume Next

    b0 = Array(192,224,240,248,252,254)
    urlcode = Replace(urlcode,"+"," ")
    pass = 0
    utf8 = -1

    length = Len(urlcode) : start = InStr(urlcode,"%") : final = InStrRev(urlcode,"%")
    If start = 0 or length < 3 Then URLDecode = urlcode : Exit Function
    leftstr = Left(urlcode,start - 1) : rightstr = Right(urlcode,length - 2 - final)

    For i = start To final
        char = Mid(urlcode,i,1)
        If char = "%" Then
            bx = URLDecode_Hex(Mid(urlcode,i + 1,2))
            If bx > 31 And bx < 128 Then
                i = i + 2
                finalstr = finalstr & ChrW(bx)
            ElseIf bx > 127 Then
                i = i + 2
                If utf8 < 0 Then
                    butf8 = 1 : blength = -1 : b1 = bx
                    For position = 4 To 0 Step -1
                        If b1 >= b0(position) And b1 < b0(position + 1) Then
                            blength = position
                            Exit For
                        End If
                    Next
                    If blength > -1 Then
                        For position = 0 To blength
                            b1 = URLDecode_Hex(Mid(urlcode,i + position * 3 + 2,2))
                            If b1 < 128 or b1 > 191 Then butf8 = 0 : Exit For
                        Next
                    Else
                        butf8 = 0
                    End If
                    If butf8 = 1 And blength = 0 Then butf8 = -2
                    If butf8 > -1 And utf8 = -2 Then i = start - 1 : finalstr = "" : pass = 1
                    utf8 = butf8
                End If
                If pass = 0 Then
                    If utf8 = 1 Then
                        b1 = bx : u = 0 : blength = -1
                        For position = 4 To 0 Step -1
                            If b1 >= b0(position) And b1 < b0(position + 1) Then
                                blength = position
                                b1 = (b1 xOr b0(position)) * 64 ^ (position + 1)
                                Exit For
                            End If
                        Next
                        If blength > -1 Then
                            For position = 0 To blength
                                bx = URLDecode_Hex(Mid(urlcode,i + 2,2)) : i = i + 3
                                If bx < 128 or bx > 191 Then u = 0 : Exit For
                                u = u + (bx And 63) * 64 ^ (blength - position)
                            Next
                            If u > 0 Then finalstr = finalstr & ChrW(b1 + u)
                        End If
                    Else
                        b1 = bx * &h100 : u = 0
                        bx = URLDecode_Hex(Mid(urlcode,i + 2,2))
                        If bx > 0 Then
                            u = b1 + bx
                            i = i + 3
                        Else
                            If Left(urlcode,1) = "%" Then
                                u = b1 + Asc(Mid(urlcode,i + 3,1))
                                i = i + 2
                            Else
                                u = b1 + Asc(Mid(urlcode,i + 1,1))
                                i = i + 1
                            End If
                        End If
                        finalstr = finalstr & Chr(u)
                    End If
                Else
                    pass = 0
                End If
            End If
        Else
            finalstr = finalstr & char
        End If
    Next
    URLDecode = leftstr & finalstr & rightstr
End Function

Function URLDecode_Hex(ByVal h)
    On Error Resume Next
    h = "&h" & Trim(h) : URLDecode_Hex = -1
    If Len(h) <> 4 Then Exit Function
    If isNumeric(h) Then URLDecode_Hex = cInt(h)
End Function
%>

第二段代码,自定义的函数是UnURLcode()
<%
function UnUrlcode(InpCode)                                   'UrlCode编码转换为字符串
   Dim Head, SSCode, I, STemp
   If Trim(InpCode) <> "" Then
      Head = True
      UnUrlcode = "" : SSCode = ""
      For I=1 to Len(InpCode)
         STemp = Mid(InpCode, I ,1)
         If STemp = "%" Then
            STemp = Mid(InpCode, I + 1, 2)
            If Head Then
               SSCode = "&H" & STemp                          '第一个%前加“&H”
               Head = False
            Else
               SSCode = SSCode + STemp
               Head = True
               UnUrlcode = UnUrlcode & chr(SSCode)
            End If
            I = I + 2
         Else
            UnUrlcode = UnUrlcode & STemp                     '非汉字直接加原字符
         End If
      Next
   End If
End Function

function Urlcode(InpStr)                                      '字符串转换为UrlCode编码
   Dim InpAsc,I
   For I = 1 To Len(InpStr)                                   '取单个字符处理
'msgbox Mid(InpStr, I, 1) & " // " & Asc(Mid(InpStr, I, 1))
      InpAsc = Asc(Mid(InpStr, I, 1))                         '单个字符的ASC码
      If ((InpAsc < 58) And (InpAsc > 47)) Or ((InpAsc < 91) And (InpAsc > 64)) Or ((InpAsc < 123) And (InpAsc > 96)) Then
         Urlcode = Urlcode & Chr(InpAsc)                      '如果是数字或字母原字符不变
      Else
         Urlcode = Urlcode & "%" & mid(Trim(Hex(InpAsc)),1,2) & "%" & mid(Trim(Hex(InpAsc)),3,2)
      End If
   Next
end function
%>

这两组自定义函数都可以正常的将urlcode解码为中文字符串,使用哪个都可以。

测试地址:http://www.icoa.cn/demo/urlcode.asp

本文链接:http://www.icoa.cn/a/482.html
关键词: urlcode   urldecode   urlencode   编码   解码  
上一篇:ASP不能用 '..' 表示父目录解决办法
下一篇:301重定向ASP代码 ASP的301重定向代码
相关文章
·eWebEditor编辑器在IE8/IE7下无法使用解决方法 2010-07-28 11:08:02
·用ASP程序将html代码转换为文本格式输出 2010-07-26 10:49:01
·通过在ASP中定义数组轻松输出今日周几 2010-07-17 15:11:30
·用ASP隐藏IP末位完整版 2010-07-10 10:43:47
·ASP错误代码表及原因 2009-08-04 08:39:35
·ASP用replace()替换字符串的用法 2011-05-21 09:11:50
·用ASP内建对象轻松做网址域名自动跳转(下) 2010-06-29 08:34:43
·ASP不能用 '..' 表示父目录解决办法 2011-05-15 17:54:24
文章评论

共有0条评论(点击查看)



4189

逍遥峡谷 逍遥乱弹 张立博 飓风团队 爱之路 酷品优选 宝贝派 张子涵


版权所有©逍遥峡谷 Copyright©Canyon of Airily 冀ICP备06033848号