ASP的URLDecode函数URLEncode解码函数

来源:互联网 时间:1970-01-01

我们知道,ASP的Server对象有个URLEncode方法可以对地址进行编码,但却没有相应的解码函数,下边的这个函数可以实现对URLEncode的解码。
下面的代码是从kesion系统扒下的,确实不错,支持utf8格式。
 

view source

print?

001'================================================ 

002'函数名:URLDecode 

003'作 用:URL解码 

004'================================================ 

005FunctionURLDecode(ByValurlcode) 

006Dimstart,final,length,char,i,butf8,pass 

007Dimleftstr,rightstr,finalstr 

008Dimb0,b1,bx,blength,position,u,utf8 

009OnErrorResumeNext 

010  

011b0 = Array(192,224,240,248,252,254) 

012urlcode = Replace(urlcode,"+"," "

013pass = 0 

014utf8 = -1 

015  

016length = Len(urlcode) : start = InStr(urlcode,"%") : final = InStrRev(urlcode,"%"

017Ifstart = 0 Orlength < 3 ThenURLDecode = urlcode : ExitFunction 

018leftstr = Left(urlcode,start - 1) : rightstr = Right(urlcode,length - 2 - final) 

019  

020Fori = start Tofinal 

021char = Mid(urlcode,i,1) 

022Ifchar = "%"Then 

023bx = URLDecode_Hex(Mid(urlcode,i + 1,2)) 

024Ifbx > 31 Andbx < 128 Then 

025i = i + 2 

026finalstr = finalstr & ChrW(bx) 

027ElseIfbx > 127 Then 

028i = i + 2 

029Ifutf8 < 0 Then 

030butf8 = 1 : blength = -1 : b1 = bx 

031Forposition = 4 To0 Step-1 

032Ifb1 >= b0(position) Andb1 < b0(position + 1) Then 

033blength = position 

034ExitFor 

035EndIf 

036Next 

037Ifblength > -1 Then 

038Forposition = 0 Toblength 

039b1 = URLDecode_Hex(Mid(urlcode,i + position * 3 + 2,2)) 

040Ifb1 < 128 Orb1 > 191 Thenbutf8 = 0 : ExitFor 

041Next 

042Else 

043butf8 = 0 

044EndIf 

045Ifbutf8 = 1 Andblength = 0 Thenbutf8 = -2 

046Ifbutf8 > -1 Andutf8 = -2 Theni = start - 1 : finalstr = "": pass = 1 

047utf8 = butf8 

048EndIf 

049Ifpass = 0 Then 

050Ifutf8 = 1 Then 

051b1 = bx : u = 0 : blength = -1 

052Forposition = 4 To0 Step-1 

053Ifb1 >= b0(position) Andb1 < b0(position + 1) Then 

054blength = position 

055b1 = (b1 xOr b0(position)) * 64 ^ (position + 1) 

056ExitFor 

057EndIf 

058Next 

059Ifblength > -1 Then 

060Forposition = 0 Toblength 

061bx = URLDecode_Hex(Mid(urlcode,i + 2,2)) : i = i + 3 

062Ifbx < 128 Orbx > 191 Thenu = 0 : ExitFor 

063u = u + (bx And63) * 64 ^ (blength - position) 

064Next 

065Ifu > 0 Thenfinalstr = finalstr & ChrW(b1 + u) 

066EndIf 

067Else 

068b1 = bx * &h100 : u = 0 

069bx = URLDecode_Hex(Mid(urlcode,i + 2,2)) 

070Ifbx > 0 Then 

071u = b1 + bx 

072i = i + 3 

073Else 

074IfLeft(urlcode,1) = "%"Then 

075u = b1 + Asc(Mid(urlcode,i + 3,1)) 

076i = i + 2 

077Else 

078u = b1 + Asc(Mid(urlcode,i + 1,1)) 

079i = i + 1 

080EndIf 

081EndIf 

082finalstr = finalstr & Chr(u) 

083EndIf 

084Else 

085pass = 0 

086EndIf 

087EndIf 

088Else 

089finalstr = finalstr & char 

090EndIf 

091Next 

092URLDecode = leftstr & finalstr & rightstr 

093EndFunction 

094  

095FunctionURLDecode_Hex(ByValh) 

096OnErrorResumeNext 

097h = "&h"& Trim(h) : URLDecode_Hex = -1 

098IfLen(h) <> 4 ThenExitFunction 

099IfisNumeric(h) ThenURLDecode_Hex = cInt(h) 

100EndFunction

相关阅读:
Top