解决ASP中传送中文参数乱码的问题

2014 年 11 月 1 日2930

在网页中,有时候会传送一些中文参数,但是在某些浏览器下(比如IE,在Firefox就是正常的),这些中文参数转换就会出错,会用到ASP中的URLEncode方法来解决这个问题。

比如传送的参数为:

http://http://www.zjjv.com///?西部e网

直接发送可能在程序接受方就是乱码,尤其在UFT-8的编码下,所以我们发送前要先转换一下

<% Response.Write "/?"&Server.URLEncode("西部e网") %>

转换后就变成了

http://http://www.zjjv.com///?id=%E8%A5%BF%E9%83%A8e%E7%BD%91

发送过去接收的程序还要将%E8%A5%BF%E9%83%A8e%E7%BD%91这部分转换回原来的代码,这个时候ASP就没有提供一个解码的方法了,要我们自己写一个:

<%
'Server.URLEncode(string)的解密函数
Function URLDecode(enStr)
dim deStr,strSpecial
dim c,i,v
deStr=""
strSpecial="!""#$%&'()*+,.-_/:;<=>?@[\]^`{|}~%"
for i=1 to len(enStr)
c=Mid(enStr,i,1)
if c="%" then
v=eval("&h"+Mid(enStr,i+1,2))
if inStr(strSpecial,chr(v))>0 then
deStr=deStr&chr(v)
i=i+2
else
v=eval("&h"+ Mid(enStr,i+1,2) + Mid(enStr,i+4,2))
deStr=deStr & chr(v)
i=i+5
end if
else
if c="+" then
deStr=deStr&" "
else
deStr=deStr&c
end if
end if
next
URLDecode=deStr
End function
%>

接受的时候这样写:

<% name = URLEncode(Request("id")) %>

这样就转换过来了!

0 0