come on babay

come on babay,Love me ,love yourself.

2007/05/15

 

关于Server.URLencode()的一些问题

-- 我用Server.URLencode("正常登陆")将中文存入数据库中,得到%D5%FD%B3%A3%B5%C7%C2%BD,那么我从数据库中读出来的时候应该怎么处理,才能得到正确的中文

1楼

贴一段:

可以用javascript
a=decodeURI(%BC%BC%CA%F5%C2%DB%CC%B3)这个就是
如果你要在ASP里服务器端的VBSCRIPT里得到
可以用下面的函数
<%
'解码函数
Function URLDecode(enStr)
dim deStr
dim c,i,v
deStr=""
for i=1 to len(enStr)
c=Mid(enStr,i,1)
if c="%" then
v=eval("&h"+Mid(enStr,i+1,2))
if v<128 then
deStr=deStr&chr(v)
i=i+2
else
if isvalidhex(mid(enstr,i,3)) then
if isvalidhex(mid(enstr,i+3,3)) then
v=eval("&h"+Mid(enStr,i+1,2)+Mid(enStr,i+4,2))
deStr=deStr&chr(v)
i=i+5
else
v=eval("&h"+Mid(enStr,i+1,2)+cstr(hex(asc(Mid(enStr,i+3,1)))))
deStr=deStr&chr(v)
i=i+3
end if
else
destr=destr&c
end if
end if
else
if c="+" then
deStr=deStr&" "
else
deStr=deStr&c
end if
end if
next
URLDecode=deStr
end function

function isvalidhex(str)
isvalidhex=true
str=ucase(str)
if len(str)<>3 then isvalidhex=false:exit function
if left(str,1)<>"%" then isvalidhex=false:exit function
c=mid(str,2,1)
if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then isvalidhex=false:exit function
c=mid(str,3,1)
if not (((c>="0") and (c<="9")) or ((c>="A") and (c<="Z"))) then isvalidhex=false:exit function
end function


'使用方式:
response.write urldecode("%BC%BC%CA%F5%C2%DB%CC%B3")
%>

今天调个下载程序。当下载的URL中含有中文时候,无法下载,比如:
http://www.huachu.com.cn/itbook/booklist.asp?tsmc=汇编
我就用 .net 中的 Server.UrlEncode 函数进行转换。但是这样仍然不行。试验了很久也没有找到原因。后来怀疑 ASP.net中的Server.UrlEncode函数和ASP中的Server.URLEncode函数返回的值竟然不一样。一实验。竟然确实是。
试验代码:
ASP.net 中 如下代码? Response.Write(Server.UrlEncode("汇编")); ?返回: %e6%b1%87%e7%bc%96
ASP 中 如下代码 Response.Write Server.URLEncode("汇编")?? 返回: %BB%E3%B1%E0
产生这个问题的原因:ASP.net 中的 Server.UrlEncode 默认是按照 UTF-8 编码方式进行处理的。而ASP中是按照本地设置编码方式进行处理的。
如果你在 ASP.net 下采用如下的编码: ASP 和 ASP.net 的结果就会一样:
Response.Write(HttpUtility.UrlEncode("汇编",Encoding.Default));
采用:Response.Write(HttpUtility.UrlEncode("汇编",Encoding.UTF8));? 返回的就是 Response.Write(Server.UrlEncode("汇编"));?? 返回的结果。
?

我的网络营销boogger
http://china-news-it.blogspot.com





<< 主页

存档

2007/4/8 - 2007/4/15   2007/4/15 - 2007/4/22   2007/4/22 - 2007/4/29   2007/4/29 - 2007/5/6   2007/5/6 - 2007/5/13   2007/5/13 - 2007/5/20   2007/5/20 - 2007/5/27   2007/5/27 - 2007/6/3   2007/6/3 - 2007/6/10   2007/6/10 - 2007/6/17   2007/6/17 - 2007/6/24   2007/6/24 - 2007/7/1   2007/7/8 - 2007/7/15   2007/12/2 - 2007/12/9   2007/12/9 - 2007/12/16   2007/12/16 - 2007/12/23   2009/10/18 - 2009/10/25   2010/1/10 - 2010/1/17  

This page is powered by Blogger. Isn't yours?

订阅 博文 [Atom]