C#:Cookie设置
记录工作中遇到的一些Cookie设置的问题:
-
异常:参数“{0}”不能是空字符串。参数名: cookie.Domain
解决方案:在Cookie设置中加上你访问的域名,例:将Cookie设置
Cookie cookie = new Cookie("name_xxxx","value_xxxx");
修改为
Cookie cookie = new Cookie("name_xxxx","value_xxxx", "/", ".***.com");
-
异常:cookie 的“Value”=“xxxxx,xxxxx”部分无效
解决方案:在使用的时候可以将逗号替换为%2C写入Cookie中 。例:
Cookie cookie = new Cookie("test","45,46");
需要修改为
Cookie cookie = new Cookie("test","45%2C46");
或者直接调用System.Web.HttpUtility.UrlEncode("45,46")转换:
Cookie cookie = new Cookie("test", System.Web.HttpUtility.UrlEncode("45,46"));