以前开开发过程中使用cookie比较随便,后来发现同一个cookie在不同端口的站点下面其实是共有的,给开发造成一定的麻烦。现在我的cookie访问类已经修改过来了。所以说,有时候网上的东西还是不能拿过来就用啊,具体问题还得具体分析
1 using System; 2 using System.Web; 3 namespace Common.Web 4 { 5 /// <summary> 6 /// 操作Cookie 7 /// </summary> 8 public class Cookie 9 { 10 11 /// <summary> 12 /// 创建Cookies 13 /// </summary> 14 /// <param name="strName">Cookie 主键</param> 15 /// <param name="strValue">Cookie 键值</param> 16 /// <param name="strDay">Cookie 天数(单位:30分钟)</param> 17 /// <code>Cookie ck = new Cookie();</code> 18 /// <code>ck.setCookie("主键","键值","天数");</code> 19 public bool SetCookie(string strName, string strValue, int strDay) 20 { 21 try 22 { 23 DelCookie(strName); 24 string port = HttpContext.Current.Request.Url.Port.ToString(); 25 strName = strName + "_" + port; 26 HttpCookie Cookie = new HttpCookie(strName); 27 Cookie.Expires = DateTime.Now.AddDays(strDay / 48.0); 28 // Cookie.Expires = DateTime.Now.AddDays(strDay / 1.0); 29 Cookie.Value = strValue; 30 System.Web.HttpContext.Current.Response.Cookies.Add(Cookie); 31 return true; 32 } 33 catch 34 { 35 // Common.LogManager.GetInstance().Log("设置COOKIE失败"); 36 return false; 37 } 38 } 39 40 /// <summary> 41 /// 读取Cookies 42 /// </summary> 43 /// <param name="strName">Cookie 主键</param> 44 /// <code>Cookie ck = new Cookie();</code> 45 /// <code>ck.getCookie("主键");</code> 46 public string GetCookie(string strName) 47 { 48 string port = HttpContext.Current.Request.Url.Port.ToString(); 49 strName = strName + "_" + port; 50 HttpCookie Cookie = System.Web.HttpContext.Current.Request.Cookies[strName]; 51 if (Cookie != null) 52 { 53 if (Cookie.Value != null) 54 { 55 return Cookie.Value.ToString(); 56 } 57 else 58 { 59 return null; 60 } 61 } 62 else 63 { 64 return null; 65 } 66 } 67 68 /// <summary> 69 /// 删除Cookies 70 /// </summary> 71 /// <param name="strName">Cookie 主键</param> 72 /// <code>Cookie ck = new Cookie();</code> 73 /// <code>ck.delCookie("主键");</code> 74 public bool DelCookie(string strName) 75 { 76 try 77 { 78 string port = HttpContext.Current.Request.Url.Port.ToString(); 79 strName = strName + "_" + port; 80 HttpCookie Cookie = new HttpCookie(strName); 81 Cookie.Expires = DateTime.Now.AddDays(-100); 82 Cookie.Value = null; 83 System.Web.HttpContext.Current.Response.Cookies.Add(Cookie); 84 return true; 85 } 86 catch 87 { 88 return false; 89 } 90 } 91 /// <summary> 92 /// 得到网站的Cookie 93 /// </summary> 94 /// <param name="url">网站域名 如http://www.xxx.com</param> 95 /// <param name="cookie"></param> 96 /// <returns></returns> 97 public System.Net.CookieCollection GetCookieCollection(string url, System.Net.CookieContainer cookie) 98 { 99 Uri uri = new Uri(url); 100 System.Net.CookieCollection ccll = cookie.GetCookies(uri); 101 return ccll; 102 } 103 104 } 105 }
1 function getcookie(varName) { 2 var port = location.port; //浏览器端口 3 if (port == "") { 4 port = "80"; 5 } 6 var cookieStr = document.cookie; 7 if (cookieStr == "") { 8 return "0"; 9 } 10 var cookieValue = cookieStr.split(";"); 11 //alert(cookieValue); 12 var startPos = -1; 13 var endPos = -1; 14 for (var i = 0; i < cookieValue.length; i++) { 15 //startPos = cookieValue[i].indexOf(varName); 16 var key = cookieValue[i].substring(0, cookieValue[i].indexOf("=")) 17 //alert(("查询到的KEY " + key).toString().length); 18 //alert(("希望查询到的KEY " + varName + "_" + port).toString().length); 19 if (key.replace(" ", "") == (varName + "_" + port).replace(" ", "")) { 20 startPos = cookieValue[i].indexOf("=")+1 21 var css = cookieValue[i].substring(startPos); 22 //alert(varName + ":" + port + "=" + css); 23 //alert("查询 "+varName+" 得到 "+css); 24 return css; 25 } 26 } 27 28 return "0"; 29 } 30 function savecookie(key, val) { 31 var port = location.port; 32 if (port == "") { 33 port = "80"; 34 } 35 var the_date = new Date("December 31, 2020"); 36 var expiresDate = the_date.toGMTString(); 37 document.cookie = key + "_" + port + "=" + escape(val) + "; expires=" + expiresDate; 38 } 39 function delCookie(name) { 40 var port = location.port; 41 if (port == "") { 42 port = "80"; 43 } 44 var exp = new Date(); 45 exp.setTime(exp.getTime() - 1000); 46 var cval = getcookie(name); 47 document.cookie = name + "_" + port + "=" + escape(cval) + ";expires=" + exp.toGMTString(); 48 } 49 function getPort(str) { 50 s = str.split('_'); 51 return s[1]; 52 }