用ASP应用程序实现自己的UrlDeCode
2023-06-13 09:13:41 时间
即:
如果有空格就用%20代替,如果有其它字符就用%ASCII代替,如果有汉字等四个字节的字符,就用两个%ASCII来代替。不过有时候我们也需要将经过这种编码的字符串进行解码,但asp并没有提供相关的函数,这给我们处理问题带来了一定的麻烦。其实我们只要知道了编码规则后,就可以用asp代码来实现我们自己的URlDecode函数了。
具体实现如下:
复制代码代码如下:
如果有空格就用%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
相关文章
- ASP结合MySQL:实现快速数据库连接(asp如何连接mysql)
- .net搭建ASP.NET应用程序在Linux系统上(linux搭建asp)
- .net安装 Linux 上的 ASP.NET 环境(linux安装asp)
- 让Linux支持ASP:开启技术新时代(让linux支持asp)
- ASP连接MySQL实现数据交互的操作语句(asp连接mysql语句)
- ASP程序连接MySQL完美实现本地数据库操作(asp连接mysql地址)
- ASP实现MySQL数据库连接及编写代码(asp连接mysql代码)
- ASP读取MySQL时间的方式(asp读取mysql时间)
- ASP操作MySQL数据库实现简单便捷功能(asp用mysql数据库)
- ASP编程查询MySQL之道(asp查询mysql)
- 使用MySQL轻松实现ASP数据库应用(asp数据库用mysql)
- ASP技术点缀MySQL之美展示精彩绚丽(asp展示mysql)
- 用ASP编写MySQL数据库程序简单又高效(asp使用mysql代码)
- MySQL与ASP结合,为网站带来绿色发展(asp mysql 绿色)
- ASP操作Oracle6实现最佳数据库连接体验(asp连接oracle6)
- ASP驱动的Oracle数据库连接源码解析(asp连oracle源码)
- ASP技术保障Oracle数据库安全备份(asp备份oracle)
- asp.net简化接收参数值的函数
- asp.netFCKeditor自定义非空验证
- ASP.NET动态生成静态页面的实例代码
- ASP.NET刷新页面的几种方法详解