您的位置:首页 > 编程语言 > ASP

asp.net中Cookie的简单用法

2011-10-27 17:44 399 查看
Cookie是什么? Cookie 是一小段文本信息,伴随着用户请求和页面在 Web 服务器和浏览器之间传递。Cookie 包含每次用户访问站点时 Web 应用程序都可以读取的信息。

为什么需要Cookie? 因为HTTP协议是无状态的,对于一个浏览器发出的多次请求,WEB服务器无法区分是不是来源于同一个浏览器。所以,需要额外的数据用于维护会话。Cookie 正是这样的一段随HTTP请求一起被传递的额外数据。

Cookie能做什么? Cookie只是一段文本,所以它只能保存字符串。而且浏览器对它有大小限制以及它会随着每次请求被发送到服务器,所以应该保证它不要太大。Cookie的内容也是明文保存的,有些浏览器提供界面修改,所以,不适合保存重要的或者涉及隐私的内容。

Cookie 的限制。 大多数浏览器支持最大为 4096 字节的 Cookie。由于这限制了 Cookie 的大小,最好用 Cookie 来存储少量数据,或者存储用户 ID 之类的标识符。用户 ID 随后便可用于标识用户,以及从数据库或其他数据源中读取用户信息。浏览器还限制站点可以在用户计算机上存储的 Cookie 的数量。大多数浏览器只允许每个站点存储 20 个 Cookie;如果试图存储更多 Cookie,则最旧的 Cookie 便会被丢弃。有些浏览器还会对它们将接受的来自所有站点的
Cookie 总数作出绝对限制,通常为 300 个。

写了一个简单的cookie例子

客户端代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="cookie.aspx.cs" Inherits="cookie" %>

<!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>Cookie 初步使用</title>
<script type="text/javascript">
function wirteCookie(){
var cookie=" cookie_name=sssssssssssss;path=/";
document.cookie=cookie;
}

function readCookie(){
alert(document.cookie);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>
<asp:Button  Text="第一个获取Cookie" runat="server" ID="btnFirst" OnClick="btnFirst_Click"/>
<asp:Button ID="btnSecond"  Text="Cookie保存自定义对象" runat="server" OnClick="btnSecond_Click"/>
<asp:Label ID="lblCookie" runat="server"></asp:Label>
</p>
<p>
<input type="button" value="js写入cookie" onclick="wirteCookie();" />
<input type="button" value="js读取cookie" onclick="readCookie();" />
</p>
</div>
</form>
</body>
</html>


服务端代码:

using System;
using System.Web;

public partial class cookie : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//设置cookie
HttpCookie cok = new HttpCookie("MyFirstCookie","第一次测试哈");
Response.Cookies.Add(cok);

WriteCookie();
}

/// <summary>
/// 写入cookie
/// </summary>
private void WriteCookie()
{
person person = new person(24, "Alice");
HttpCookie cookie = new HttpCookie("PersonSelf");
cookie["age"] = person.Age.ToString();
cookie["name"] = person.Name.ToString();
Response.Cookies.Add(cookie);
}

private void ReadCookie() {
HttpCookie cookie = Request.Cookies["PersonSelf"];
if (cookie == null)
{
this.lblCookie.Text = "未定义";
}
else
{
person p = new person();
p.Age = Convert.ToInt32(cookie["age"]);
p.Name = cookie["name"];
this.lblCookie.Text = p.ToString();
}
}

protected void btnFirst_Click(object sender, EventArgs e)
{
//读取Cookie
HttpCookie cookie = Request.Cookies["MyFirstCookie"];
if (cookie == null)
this.lblCookie.Text = "cookie 还未定义";
else
this.lblCookie.Text = cookie.Value;
}

protected void btnSecond_Click(object sender, EventArgs e)
{
ReadCookie();
}
}


Person类:

using System;
using System.Web;
using System.Web.Script.Serialization;  //reference System.Web.Extensions.dll

/// <summary>
/// Summary description for person
/// </summary>
public class person
{
public person() { }
public person(int age,string name)
{
this.age = age;
this.name = name;
}

private int age;

public int Age
{
get { return age; }
set { age = value; }
}

private string name;

public string Name
{
get { return name; }
set { name = value; }
}

public override string ToString()
{
return string.Format("age={0},name={1}", this.age, this.name);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: