asp URLDecode支持汉字(转)

可以与javascript的encodeURIComponent配合编码汉字

 

Function URLDecode(strIn)
dim tempCode
tempCode=Session.CodePage
'设置url编码类型为utf-8
Session.CodePage = 65001

tURLDecode = ""
Dim sl: sl = 1
Dim tl: tl = 1
Dim key: key = "%"
Dim kl: kl = Len(key)
sl = InStr(sl, strIn, key, 1)
Do While sl>0
If (tl=1 And sl<>1) Or tl<sl Then
tURLDecode = tURLDecode & Mid(strIn, tl, sl-tl)
End If
Dim hh, hi, hl
Dim a
Select Case UCase(Mid(strIn, sl+kl, 1))
Case "U":'Unicode URLEncode
a = Mid(strIn, sl+kl+1, 4)
tURLDecode = tURLDecode & ChrW("&H" & a)
sl = sl + 6
Case "E":'UTF-8 URLEncode
hh = Mid(strIn, sl+kl, 2)
a = Int("&H" & hh)'ascii码
If Abs(a)<128 Then
sl = sl + 3
tURLDecode = tURLDecode & Chr(a)
Else
hi = Mid(strIn, sl+3+kl, 2)
hl = Mid(strIn, sl+6+kl, 2)
a = ("&H" & hh And &H0F) * 2 ^12 Or ("&H" & hi And &H3F) * 2 ^ 6 Or ("&H" & hl And &H3F)
If a<0 Then a = a + 65536
tURLDecode = tURLDecode & ChrW(a)
sl = sl + 9
End If
Case Else:'Asc URLEncode
hh = Mid(strIn, sl+kl, 2)'高位
a = Int("&H" & hh)'ascii码
If Abs(a)<128 Then
sl = sl + 3
Else
hi = Mid(strIn, sl+3+kl, 2)'低位
a = Int("&H" & hh & hi)'非ascii码
sl = sl + 6
End If
tURLDecode = tURLDecode & Chr(a)
End Select
tl = sl
sl = InStr(sl, strIn, key, 1)
Loop
tURLDecode = tURLDecode & Mid(strIn, tl)
URLDecode=tURLDecode

Session.CodePage = tempCode
 
End function

posted @ 2011-07-28 13:28  viewweb  阅读(827)  评论(0编辑  收藏  举报