佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 用ASP應用程序實現(xiàn)自己的UrlDeCode

用ASP應用程序實現(xiàn)自己的UrlDeCode

熱門標簽:團購網(wǎng)站 Mysql連接數(shù)設置 電子圍欄 Linux服務器 科大訊飛語音識別系統(tǒng) 銀行業(yè)務 服務器配置 阿里云
即:  
  如果有空格就用%20代替,如果有其它字符就用%ASCII代替,如果有漢字等四個字節(jié)的字符,就用兩個%ASCII來代替。不過有時候我們也需要將經(jīng)過這種編碼的字符串進行解碼,但asp并沒有提供相關的函數(shù),這給我們處理問題帶來了一定的麻煩。其實我們只要知道了編碼規(guī)則后,就可以用asp代碼來實現(xiàn)我們自己的URlDecode函數(shù)了。
具體實現(xiàn)如下:  
復制代碼 代碼如下:

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

下面為大家提供一個更成熟的函數(shù):
復制代碼 代碼如下:

'================================================
'函數(shù)名: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
您可能感興趣的文章:
  • ASP中實現(xiàn)的URLEncode、URLDecode自定義函數(shù)
  • ASP的URLDecode函數(shù)URLEncode解碼函數(shù)
  • ASP中只有UrlEncode,沒有Urldecode問題的解決方法?

標簽:江蘇 蚌埠 棗莊 大理 廣元 萍鄉(xiāng) 衢州 衡水

巨人網(wǎng)絡通訊聲明:本文標題《用ASP應用程序實現(xiàn)自己的UrlDeCode》,本文關鍵詞  ;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    东方市| 治多县| 镇平县| 延安市| 邯郸市| 瓦房店市| 临汾市| 保定市| 类乌齐县| 延安市| 安图县| 马公市| 县级市| 宁远县| 南投县| 乐平市| 旬邑县| 台中市| 贺兰县| 上虞市| 那坡县| 都江堰市| 永嘉县| 东至县| 东乌珠穆沁旗| 巴中市| 宝坻区| 通州区| 炉霍县| 鲁甸县| 阳山县| 安丘市| 卢湾区| 延安市| 铜陵市| 凯里市| 茶陵县| 怀安县| 牙克石市| 方城县| 金坛市|