用ASP应用程序实现自己的UrlDeCode
程序员文章站
2022-06-21 23:19:07
即: 如果有空格就用%20代替,如果有其它字符就用%ascii代替,如果有汉字等四个字节的字符,就用两个%ascii来代替。不过有时候我们也需要...
即:
如果有空格就用%20代替,如果有其它字符就用%ascii代替,如果有汉字等四个字节的字符,就用两个%ascii来代替。不过有时候我们也需要将经过这种编码的字符串进行解码,但asp并没有提供相关的函数,这给我们处理问题带来了一定的麻烦。其实我们只要知道了编码规则后,就可以用asp代码来实现我们自己的urldecode函数了。
具体实现如下:
function urldecode(encodestr)
newstr=""
havechar=false
lastchar=""
for i=1 to len(encodestr)
char_c=mid(encodestr,i,1)
if char_c="+" then
newstr=newstr & " "
elseif char_c="%" then
next_1_c=mid(encodestr,i+1,2)
next_1_num=cint("&h" & next_1_c)
if havechar then
havechar=false
newstr=newstr & chr(cint("&h" & lastchar & next_1_c))
else
if abs(next_1_num)<=127 then
newstr=newstr & chr(next_1_num)
else
havechar=true
lastchar=next_1_c
end if
end if
i=i+2
else
newstr=newstr & char_c
end if
next
urldecode=newstr
end function
下面为大家提供一个更成熟的函数:
'================================================
'函数名:urldecode
'作 用:url解码
'================================================
function urldecode(byval urlcode)
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
如果有空格就用%20代替,如果有其它字符就用%ascii代替,如果有汉字等四个字节的字符,就用两个%ascii来代替。不过有时候我们也需要将经过这种编码的字符串进行解码,但asp并没有提供相关的函数,这给我们处理问题带来了一定的麻烦。其实我们只要知道了编码规则后,就可以用asp代码来实现我们自己的urldecode函数了。
具体实现如下:
复制代码 代码如下:
function urldecode(encodestr)
newstr=""
havechar=false
lastchar=""
for i=1 to len(encodestr)
char_c=mid(encodestr,i,1)
if char_c="+" then
newstr=newstr & " "
elseif char_c="%" then
next_1_c=mid(encodestr,i+1,2)
next_1_num=cint("&h" & next_1_c)
if havechar then
havechar=false
newstr=newstr & chr(cint("&h" & lastchar & next_1_c))
else
if abs(next_1_num)<=127 then
newstr=newstr & chr(next_1_num)
else
havechar=true
lastchar=next_1_c
end if
end if
i=i+2
else
newstr=newstr & char_c
end if
next
urldecode=newstr
end function
下面为大家提供一个更成熟的函数:
复制代码 代码如下:
'================================================
'函数名:urldecode
'作 用:url解码
'================================================
function urldecode(byval urlcode)
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
上一篇: 你这是在玩我吗
下一篇: 习惯性就给你了最大的