LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

ASP中只有UrlEncode,没有Urldecode问题的解决方法?

admin
2022年6月21日 15:15 本文热度 1297

在ASP中传递参数时有一个很有用的系统函数Server.UrlEncode,可以将一些非字母数字、中文等符号转换成标准URL编码(其实就是16进制ASC码),这样就解决了参数传递乱码的问题。

Server.URLEncode("内容")

然后我以为也提供了Server.UrlDecode,但使用后却发现程序报错,原来系统并没有提供这个我想象中的解码函数。怎幺办,自己动手吧。

UrlEncode的原理其实很简单,就是将特殊字符转换成16进制ASC码值,那么译码函数就只要将16进制ASC转回对应的字符就OK了。

这是推荐的解码方法:

function URLDecode(strIn)
  URLDecode = ""
  Dim sl: sl = 1
  Dim tl: tl = 1
  Dim key: key = "%"
  Dim kl: kl = Len(key)
  sl = InStr(sl, strIn, key, 1)
  Do While sl>0
    If (tl=1 And sl<>1) Or tl<sl Then
        URLDecode = URLDecode & Mid(strIn, tl, sl-tl)
    End If
    Dim hh, hi, hl
    Dim a
    select Case UCase(Mid(strIn, sl+kl, 1))
      Case "U":                  'Unicode URLEncode
        a = Mid(strIn, sl+kl+1, 4)
        URLDecode = URLDecode & ChrW("&H" & a)
        sl = sl + 6
      Case "E":                   'UTF-8 URLEncode
        hh = Mid(strIn, sl+kl, 2)
        a = Int("&H" & hh)          'ascii码
        If Abs(a)<128 Then
          sl = sl + 3
          URLDecode = URLDecode & Chr(a)
        Else
          hi = Mid(strIn, sl+3+kl, 2)
          hl = Mid(strIn, sl+6+kl, 2)
          a = ("&H" & hh And &H0F) * 2 ^12 Or ("&H" & hi And &H3F) * 2 ^ 6 Or ("&H" & hl And &H3F)
          If a<0 Then a = a + 65536
          URLDecode = URLDecode & ChrW(a)
          sl = sl + 9
        End If
      Case Else:                      'Asc URLEncode
        hh = Mid(strIn, sl+kl, 2)   '高位
        a = Int("&H" & hh)          'ascii码
        If Abs(a)<128 Then
        sl = sl + 3
        Else
        hi = Mid(strIn, sl+3+kl, 2) '低位
        a = Int("&H" & hh & hi)     '非ascii码
        sl = sl + 6
        End If
        URLDecode = URLDecode & Chr(a)
    End select
    tl = sl
    sl = InStr(sl, strIn, key, 1)
  Loop
  URLDecode = URLDecode & Mid(strIn, tl)
End function

下面是另外一种解码方法:

Function URLDecode(enStr)                    'URL解码函数
  dim deStr
  dim c,i,v
  deStr=""
  for i=1 to len(enStr)
    c=Mid(enStr,i,1)
    if c="%" then
      v=eval("&h"+Mid(enStr,i+1,2))
      if v<128 then
        deStr=deStr&chr(v)
        i=i+2
      else
        if isvalidhex(mid(enstr,i,3)) then
          if isvalidhex(mid(enstr,i+3,3)) then
            v=eval("&h"+Mid(enStr,i+1,2)+Mid(enStr,i+4,2))
            deStr=deStr&chr(v)
            i=i+5
          else
            v=eval("&h"+Mid(enStr,i+1,2)+cstr(hex(asc(Mid(enStr,i+3,1)))))
            deStr=deStr&chr(v)
            i=i+3
          end if
        else
          destr=destr&c
        end if
      end if
    else
      if c="+" then
        deStr=deStr&" "
      else
        deStr=deStr&c
      end if
    end if
  next
  URLDecode=deStr
end function


function isvalidhex(str)
  isvalidhex=true
  str=ucase(str)
  if len(str)<>3 then isvalidhex=false:exit function
  if left(str,1)<>"%" then isvalidhex=false:exit function
  c=mid(str,2,1)
  if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then isvalidhex=false:exit function
  c=mid(str,3,1)
  if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then isvalidhex=false:exit function
end function

经测试gb312格式的asp使用没有问题。

下面是另外一种方法,如果上面的方法出错,那么就用下面这个试试:

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

该文章在 2024/11/16 17:56:15 编辑过

全部评论1

admin
2023年10月23日 11:19

 又一种算法:

Public Function URLDecode(ByVal data, ByVal charset)
  Dim strm
  Set strm = Server.createObject("ADODB.Stream")
  With strm
    .type = 2
    .charset = "iso-8859-1"
    .open
    .writeText unescape(data)
    .position = 0
    .charset = charset
    URLDecode = .readText(-1)
    .close
  End With
  Set strm = Nothing
End Function


Response.Write URLDecode("%B4%F3%BC%D2%BA%C3%B0%A1", "GB2312") & "<br/>"
Response.Write URLDecode("%E5%A4%A7%E5%AE%B6%E5%A5%BD%E5%95%8A", "UTF-8") & "<br/>"
Response.Write URLDecode("http%3A%2F%2Fwww.dangdang.com%2F", "UTF-8") & "<br/>"

该评论在 2024/11/16 17:57:53 编辑过
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved