这个问题比较热门的说~ASP 的 Server 对象提供了一个 Server.Encode() 方法,但是却没有相应的 Server.Decode() 方法或者 Unencode() 方法 >_< 也许 M$ 认为这没必要……大错特错啊。这样的方法非常有用,这里提供一种实现~包括 VBScript 和 JScript 代码。
VBScript
<%
Function URLDecode(str)
str = Replace(str, "+", " ")
For i = 1 To Len(str)
sT = Mid(str, i, 1)
If sT = "%" Then
If i+2 < Len(str) Then
sR = sR & _
Chr(CLng("&H" & Mid(str, i+1, 2)))
i = i+2
End If
Else
sR = sR & sT
End If
Next
URLDecode = sR
End Function
Function URLEncode(str)
URLEncode = Server.URLEncode(str)
End Function
str1 = "http://www.foo.com/blah.asp?foo=1 & 2 &g=0"
str2 = URLEncode(str1)
str3 = URLDecode(str2)
Response.Write(str1 & "<br>" & str2 & "<br>" & str3)
%>
JScript
<script language=JScript runat=server>
function URLDecode(str)
{
return unescape(str);
}
function URLEncode(str)
{
str = escape(str);
// JScript doesn't think '/' needs to be escaped...
// I'm not sure it does either, but take it out to be
// consistent with VBScript's built-in URLEncode()
while (str.indexOf("/")!=-1)
{
str = str.replace("/","%2F");
}
return str;
}
var str1 = "http://www.foo.com/blah.asp?foo=1 & 2 &g=0";
var str2 = URLEncode(str1);
var str3 = URLDecode(str2);
Response.Write(str1 + "<br>" + str2 + "<br>" + str3)
</script>