zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

用ASP应用程序实现自己的UrlDeCode

ASP应用程序 实现 自己 UrlDecode
2023-06-13 09:13:41 时间
即:  
  如果有空格就用%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解码
"================================================
FunctionURLDecode(ByValurlcode)
Dimstart,final,length,char,i,butf8,pass
Dimleftstr,rightstr,finalstr
Dimb0,b1,bx,blength,position,u,utf8
OnErrorResumeNext

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

length=Len(urlcode):start=InStr(urlcode,"%"):final=InStrRev(urlcode,"%")
Ifstart=0Orlength<3ThenURLDecode=urlcode:ExitFunction
leftstr=Left(urlcode,start-1):rightstr=Right(urlcode,length-2-final)

Fori=startTofinal
char=Mid(urlcode,i,1)
Ifchar="%"Then
bx=URLDecode_Hex(Mid(urlcode,i+1,2))
Ifbx>31Andbx<128Then
i=i+2
finalstr=finalstr&ChrW(bx)
ElseIfbx>127Then
i=i+2
Ifutf8<0Then
butf8=1:blength=-1:b1=bx
Forposition=4To0Step-1
Ifb1>=b0(position)Andb1<b0(position+1)Then
blength=position
ExitFor
EndIf
Next
Ifblength>-1Then
Forposition=0Toblength
b1=URLDecode_Hex(Mid(urlcode,i+position*3+2,2))
Ifb1<128Orb1>191Thenbutf8=0:ExitFor
Next
Else
butf8=0
EndIf
Ifbutf8=1Andblength=0Thenbutf8=-2
Ifbutf8>-1Andutf8=-2Theni=start-1:finalstr="":pass=1
utf8=butf8
EndIf
Ifpass=0Then
Ifutf8=1Then
b1=bx:u=0:blength=-1
Forposition=4To0Step-1
Ifb1>=b0(position)Andb1<b0(position+1)Then
blength=position
b1=(b1xOrb0(position))*64^(position+1)
ExitFor
EndIf
Next
Ifblength>-1Then
Forposition=0Toblength
bx=URLDecode_Hex(Mid(urlcode,i+2,2)):i=i+3
Ifbx<128Orbx>191Thenu=0:ExitFor
u=u+(bxAnd63)*64^(blength-position)
Next
Ifu>0Thenfinalstr=finalstr&ChrW(b1+u)
EndIf
Else
b1=bx*&h100:u=0
bx=URLDecode_Hex(Mid(urlcode,i+2,2))
Ifbx>0Then
u=b1+bx
i=i+3
Else
IfLeft(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
EndIf
EndIf
finalstr=finalstr&Chr(u)
EndIf
Else
pass=0
EndIf
EndIf
Else
finalstr=finalstr&char
EndIf
Next
URLDecode=leftstr&finalstr&rightstr
EndFunction

FunctionURLDecode_Hex(ByValh)
OnErrorResumeNext
h="&h"&Trim(h):URLDecode_Hex=-1
IfLen(h)<>4ThenExitFunction
IfisNumeric(h)ThenURLDecode_Hex=cInt(h)
EndFunction