ASP实现URL编码

2018-09-05 23:47

阅读:882

  URL编码是指为了将信息通过URL进行传输,所以必须将某些含有特殊意义的字符进行替换的一种编码方式,在asp中我们都知道有一个Server.URLEncode的函数可以完成这个功能。即:
如果有空格就用%20代替,如果有其它字符就用%ASCII代替,如果有汉字等四个字节的字符,就用两个%ASCII来代替。不过有时候我们也需要将经过这种编码的字符串进行解码,但asp并没有提供相关的函数,这给我们处理问题带来了一定的麻烦。其实我们只要知道了编码规则后,就可以用asp代码来实现我们自己的URlDecode函数了。

具体实现如下:

复制代码 代码如下:functionurldecode(encodestr)
newstr=
havechar=false
lastchar=
fori=1tolen(encodestr)
char_c=mid(encodestr,i,1)
ifchar_c=+then
newstr=newstr&
elseifchar_c=%then
next_1_c=mid(encodestr,i+1,2)
next_1_num=cint(&H&next_1_c)

ifhavecharthen
havechar=false
newstr=newstr&chr(cint(&H&lastchar&next_1_c))
else
ifabs(next_1_num)<=127then
newstr=newstr&chr(next_1_num)
else
havechar=true
lastchar=next_1_c
endif
endif
i=i+2
else
newstr=newstr&char_c
endif

next
urldecode=newstr
endfunction


评论


亲,登录后才可以留言!