Response.Cookies["userinfo"]["id"] = "1";
Response.Cookies["userinfo"]["name"] = "hello";我后台这样写入的COOKIES
前台如何用javascript读写COOKIES 并且提交到后台再读取呢
Response.Cookies["userinfo"]["name"] = "hello";我后台这样写入的COOKIES
前台如何用javascript读写COOKIES 并且提交到后台再读取呢
如果你的代码能编译通过(我感觉不能通过)
那么代码相当于HttpCookie cookie = Response.Cookies["userinfo"];
string id= cookie["id"];
id= "1";string name = cookie["name"];
name = "hello";从始至终都没有往Cookie写入任何东西,又何来的读取呢?
那么代码相当于 HttpCookie cookie = Response.Cookies["userinfo"];
string id= cookie["id"];
id= "1"; string name = cookie["name"];
name = "hello"; 从始至终都没有往Cookie写入任何东西,又何来的读取呢?
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title> <script language="javascript">
//根据名称和参数读取cookie值
//
// function GetCookie(name,params)
// {
// var originstr=document.cookie;
// var tmp;
// var reg=new RegExp("(?<=a=(\\w+?=\\w+?\&)*?ip=)\\w+(?=([&;])?)","gi");
// tmp=reg.exec(originstr);
// return tem;
// } function GetCookie(name)
{
m=document.cookie;
re1=new RegExp("(?!\w)"+name+"=[^;]+","");
re2=new RegExp("^"+name+"=","");
try
{
var a=m.match(re1)[0];
}
catch(e)
{
return null
}
eval("var o="+a.replace(re2,"{").replace(/&/g,"',").replace(/=/g,":\'")+"'}");
return o;
} function ReadCookie()
{
var var1=document.form1.txtName.value;
document.form1.txtReadIP.value=GetCookie(document.form1.txtName.value).IP;
}
function WriteCookie()
{
document.cookie=document.form1.txtName.value+"=IP="+document.form1.txtIP.value;
}
function ClearCookie()
{
document.cookie="";
}
</script></head>
<body>
<form id="form1" runat="server">
<table border="0" cellpadding="0" cellspacing="0" width="778px">
<tr>
<td style="height: 100px; width: 778px;" align="center" valign="middle">
Cookie示例(对IP的操作)</td>
</tr>
<tr>
<td align="center" style="width: 778px">
<br />
<asp:Label ID="Label1" runat="server" Text="名称"></asp:Label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox><br />
<asp:Label ID="Label2" runat="server" Text="IP"></asp:Label>
<asp:TextBox ID="txtIP" runat="server"></asp:TextBox><br />
<asp:Label ID="Label3" runat="server" Text="读取IP"></asp:Label>
<asp:TextBox ID="txtReadIP" runat="server"></asp:TextBox><br />
</td>
</tr>
<tr>
<td align="center" style="height: 25px; width: 778px;">
<br />
<br />
<asp:Button ID="btnRun" runat="server" Text="服务器设置Cookie" OnClick="Button1_Click" />
<asp:Button ID="btnGet" runat="server" Text="服务器读取Cookie" OnClick="Button2_Click"
Width="158px" /> <asp:Button ID="Button3" runat="server" Text="服务器清除Cookie"
OnClick="Button3_Click" />
<br />
<button type="button" onclick="WriteCookie()">
客户端设置Cookie</button>
<button type="button" onclick="ReadCookie()">
客户端读取Cookie</button>
<button type="button" onclick="ClearCookie()">
客户端清除Cookie</button><br />
</td>
</tr>
</table>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
public partial class easy_cookie : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
//写入
protected void Button1_Click(object sender, EventArgs e)
{
if (Request.Cookies[txtName.Text] == null)
{
HttpCookie cookie = new HttpCookie(txtName.Text);
cookie["IP"] = txtIP.Text;
Response.Cookies.Add(cookie);
}
else
{
Response.Cookies[txtName.Text]["IP"] = txtIP.Text;
}
string s="<a[^>]+href[^\"]+\"([^\"]+)\""; }
//读取
protected void Button2_Click(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies[txtName.Text];
if (cookie != null)
{
txtReadIP.Text = cookie["IP"];
}
}
//清除cookie
protected void Button3_Click(object sender, EventArgs e)
{
for (int i = 0; i < Request.Cookies.Count; i++)
{
HttpCookie cookie = Request.Cookies[i];
cookie.Expires = DateTime.Now.AddDays(-1);
}
//Response.Write(Request.Cookies.Count);
// HttpCookie cookie = Request.Cookies;
//cookie.Expires = DateTime.Now.Add(-1);
}
/// <summary>
/// 匹配结果符合数
/// </summary>
/// <param name="PatternStr">正则表达式</param>
/// <param name="ObjectStr">目标字符串</param>
/// <returns></returns>
int MatchCount(string PatternStr, string ObjectStr)
{
Regex r = new Regex(PatternStr,RegexOptions.IgnoreCase);
Match m = r.Match(ObjectStr);
int i = 0;
while (m.Success)
{
i++;
m = m.NextMatch();
}
return i;
}
}
//如果不存在这样的 cookie,就返回 cookieName
function ResponseCookies_GetCookieName(cookieName)
{
var lowerCookieName = cookieName.toLowerCase();
var cookieStr = document.cookie;
if (cookieStr == "")
{
return cookieName;
}
var cookieArr = cookieStr.split("; ");
var pos = -1;
for (var i=0; i<cookieArr.length; i++)
{
pos = cookieArr[i].indexOf("=");
if (pos > 0)
{
if (cookieArr[i].substring(0, pos).toLowerCase() == lowerCookieName)
{
return cookieArr[i].substring(0, pos);
}
}
}
return cookieName;
}