ASP URL反编码函数代码

来源:asp之家 时间:2011-02-28 11:04:00 

例如:
我们在百度中搜索 词典网,则网址后面的参数就是
http://www.baidu.com/s?cl=3&wd=%B4%CA%B5%E4%CD%F8

如果我们想将%B4%CA%B5%E4%CD%F8还原为中文,使用下面的函数即可。
<%
response.write urldecode("http://www.baidu.com/s?cl=3&wd=%B4%CA%B5%E4%CD%F8")
'================================================
'函数名: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,URL反编码
0
投稿

猜你喜欢

  • golang mysql的连接池的具体使用

    2024-01-14 11:52:10
  • MySQL 事务概念与用法深入详解

    2024-01-14 02:56:06
  • 解决mysql不能插入中文Incorrect string value

    2009-07-30 09:02:00
  • python中namedtuple函数的用法解析

    2023-08-22 11:03:24
  • Golang项目在github创建release后自动生成二进制文件的方法

    2024-05-22 10:17:50
  • python实现web方式logview的方法

    2023-12-23 17:07:54
  • 详解php中反射的应用

    2023-11-15 01:26:56
  • PyQt5实现将Matplotlib图像嵌入到Scoll Area中显示滚动条效果

    2021-12-21 05:40:39
  • 浅谈python类属性的访问、设置和删除方法

    2022-01-12 00:14:48
  • python教程十行代码教你语音转文字QQ微信聊天

    2024-01-03 09:06:54
  • MYSQL5.6.33数据库主从(Master/Slave)同步安装与配置详解(Master-Linux Slave-windows7)

    2024-01-17 00:23:34
  • SQL Server中TRUNCATE事务回滚操作方法

    2024-01-20 14:43:23
  • 在Ubuntu使用SQL Server创建Go应用程序的图文教程

    2024-01-15 10:50:50
  • Thinkphp5微信小程序获取用户信息接口的实例详解

    2023-10-26 09:57:08
  • python爬虫开发之urllib模块详细使用方法与实例全解

    2021-02-24 04:52:42
  • CSS技巧之圆角背景与三角形

    2010-10-19 12:40:00
  • Python入门之字符串操作详解

    2023-01-27 18:45:08
  • python使用pyodbc连接sqlserver

    2021-12-24 06:06:17
  • SQL处理多级分类,查询结果呈树形结构

    2012-08-21 10:50:12
  • sqlserver数据库迁移后,孤立账号解决办法

    2011-10-24 20:01:40
  • asp之家 网络编程 m.aspxhome.com