读书人

利用对象序列化将购物车保存在Cookie中

发布时间: 2011-12-27 22:22:55 作者: rapoo

利用对象序列化将购物车保存在Cookie中
最近做一个购物类网站,正好在学习对象序列化,于是突然奇想,想把购物车对象(里面已经封装了很多购物车的常用方法)序列化后,将序列化后的字符串保存在Cookie中,需要时,现从Cookie中取出该字符串,反序列化为购物车对象

主要代码如下:
购物车类ShopCart.cs:
----------------------------------------------------------------
using System;
using System.Collections;

/// <summary>
/// 购物车类
/// </summary>
[Serializable]
public class ShopCart
{
...详细内容见附件

}

以下是调用页面:
Demo.aspx
---------------------------------------------------
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "Demo.aspx.cs " Inherits= "Public_Test_Demo " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:GridView ID= "GridView1 " runat= "server ">
</asp:GridView>
<br/>
<asp:Button ID= "btnReadCookie " runat= "server " Text= "读取Cookie中的ShopCart " OnClick= "btnReadCookie_Click " />   </div>
</form>
</body>
</html>

--------------------------------------------
Code-behind端代码:Demo.aspx.cs
--------------------------------------------
using System;
using System.Collections;
using System.Web;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Text;


public partial class Public_Test_Demo : System.Web.UI.Page
{
string CookieName = "ShopCart ";

protected void Page_Load(object sender, EventArgs e)
{


if (!IsPostBack)
{
ShopCart SC = new ShopCart();
SC.AddItem( "1 ", "ProductName ", "ProductName ", 0, 0, "ProductID ", " ", 0, 0, 0, 0, "ShopId ", "TestUrl ", true);
SC.AddItem( "2 ", "jimmy ", "ProductName123 ", 0, 0, "ProductID123 ", " ", 0, 0, 0, 0, "ShopId111 ", "TestUrl23 ", true);




//将ShopCart对象写入Cookie
IFormatter fm = new BinaryFormatter();
Stream sm = new MemoryStream();
fm.Serialize(sm, SC);
sm.Seek(0, SeekOrigin.Begin);
StreamReader reader = new StreamReader(sm);
string strCart = reader.ReadToEnd();
reader.Close();

HttpCookie hc = new HttpCookie(CookieName);
hc.Value = Server.UrlEncode(strCart);
hc.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(hc);

}
}


protected void btnReadCookie_Click(object sender, EventArgs e)
{
HttpCookie hc = Request.Cookies[CookieName];
string strCart = Server.UrlDecode(hc.Value.ToString());
byte[] bt = System.Text.Encoding.Default.GetBytes(strCart);
Stream sm = new MemoryStream(bt);
IFormatter fm = new BinaryFormatter();
ShopCart SC = (ShopCart)fm.Deserialize(sm);
GridView1.DataSource = SC.CartItems;
GridView1.DataBind();
}
}

视步测试,没有什么问题,DataGrid也能正常显示出ShopCart的内容,但是问题也出现了,
SC.AddItem( "2 ", "jimmy ", "ProductName123 ", 0, 0, "ProductID123 ", " ", 0, 0, 0, 0, "ShopId111 ", "TestUrl23 ", true);



将这一行中的,第二个参数,改为中文汉字,比如
SC.AddItem( "2 ", "中华人民共和国 ", "ProductName123 ", 0, 0, "ProductID123 ", " ", 0, 0, 0, 0, "ShopId111 ", "TestUrl23 ", true);

但一改为中文后,反序列化时即报错:
-------------------------------------
无法将位于索引 0 处的字节 [D6] 由指定的代码页转换为 Unicode。
-------------------------------------


???请各位高手指点一二,欢迎交流

我的mail:yjmyzz@126.com
msn:yjmyzy@hotmail.com


附件下载地址
http://bbs.blueidea.com/attachment.php?aid=53084


[解决办法]
在添加 物品名称的时候可以进行编码 ,在读的时候解码
你可以编写一个编码和解码的方法,来完成
楼主,试试吧
可能哈西表不会出现这个问题

读书人网 >asp.net

热点推荐