ASP post to ASPX產生亂碼問題
xxx.ASP
<%
'使用hidden及action post 的方法將asp的session傳給aspx
Response.Write("<form name=t id=t action=SetSession.aspx method=post >")
Response.Write ("<input type=hidden name=suser value='" & replace(URLEncode(Session("suser")),"'","''") & "'>")
Response.Write "</FORM>"
Response.Write("<script>t.submit();</script>")
'*******************************************
'自製URLENCODE
Function URLEncode(strEnc)
Dim strChr, intAsc, strTmp, strTmp2, strRet, lngLoop
For lngLoop = 1 To Len(strEnc)
strChr = Mid(strEnc, lngLoop, 1)
intAsc = Asc(strChr)
If ((intAsc < 58) And (intAsc > 47)) Or ((intAsc < 91) And _
(intAsc > 64)) Or ((intAsc < 123) And (intAsc > 96)) Then
strRet = strRet & strChr
ElseIf intAsc = 32 Then
strRet = strRet & "+"
Else
strTmp = Hex(Asc(strChr))
strRet = strRet & "%" & Right("00" & Left(strTmp, 2), 2)
strTmp2 = Mid(strTmp, 3, 2)
If Len(strTmp) > 3 Then
If IsNumeric(Mid(strTmp, 3, 1)) Then
strRet = strRet & Chr(CInt("&H" & strTmp2))
Else
strRet = strRet & "%" & strTmp2
End If
End If
End If
Next
URLEncode = strRet
End Function
'***********************************%>
xxx.ASPX
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Text" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
//從前網頁取得所有的post,將其記錄到Session
for (int i = 0; i < Request.Form.Count; i++)
{
Session[Request.Form.GetKey(i)] = UTF8Encoding(Request.Form[i].ToString());
}
//測試所有的Session是否正常
//for (int i = 0; i < Session.Contents.Count; i++)
//{
// Response.Write("Assigned to \"" + Session.Keys[i].ToString() + "\"");
// Response.Write(" Value: " + Session[i].ToString() + "<BR>");
//}
}
private string UTF8Encoding(string strEncod) {
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
strEncod = HttpUtility.UrlDecode(strEncod, Encoding.GetEncoding("big5"));
byte[] addbyte = Encoding.GetEncoding("big5").GetBytes(strEncod);
byte[] utf8byte = Encoding.Convert(Encoding.GetEncoding("big5"), Encoding.UTF8, addbyte);
return encoding.GetString(utf8byte);
}
</script>
<script type="text/javascript" language="javascript">
wset="fullscreen=no,resizable=yes,scrollbars=yes,status=0,channelmode=0,directories=0,menubar=0";
newd=window.open("default2.asp","score",wset);
newd.moveTo (0,0);
newd.resizeTo(screen.availWidth,screen.availHeight);
newd.outerWidth=screen.availWidth;
newd.outerHeight=screen.availHeight;
self.opener="xx";
window.close();
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>