62,041
社区成员
发帖
与我相关
我的任务
分享
HttpCookie cookie = new HttpCookie("MyCook");//初使化并设置Cookie的名称
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(1, 0, 0, 0, 0);//过期时间为1分钟
cookie.Expires = dt.Add(ts); //设置过期时间
cookie.Values["city"] = id; //.Add("city", id);
cookie.Values["linkId"] = _linkId; //.Add("city", id);
cookie.Values["cityName"] = HttpUtility.UrlEncode(_userCity); //.Add("cityName", _userCity);
Response.Cookies["MyCook"].Domain = "ticket.hengdafilm.com"; //设置跨域
Response.AppendCookie(cookie);
HttpCookie cok = Request.Cookies["MyCook"]; //获取cookie
if (cok == null) //如果为空
{
HttpCookie cookie = new HttpCookie("MyCook"); //初使化并设置Cookie的名称
cookie.Domain = ".hengdafilm.com"; //设置跨域
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(1, 0, 0, 0, 0); //设置过期时间为1天
cookie.Expires = dt.Add(ts);//设置过期时间
cookie.Values["city"] = id; //添加键值
cookie.Values["linkId"] = _linkId; //添加键值
cookie.Values["cityName"] = HttpUtility.UrlEncode(_userCity); //添加键值,值为中文先转码
Response.AppendCookie(cookie);//保存MyCook
}
else //如果不为空
{
cok.Domain = ".hengdafilm.com"; //设置跨域
cok.Values["city"] = id; //修改
cok.Values["linkId"] = _linkId;//修改
cok.Values["cityName"] = HttpUtility.UrlEncode(_userCity);//修改
Response.AppendCookie(cok);//保存MyCook
}