Cookie中文乱码

yangchun1213 2011-04-19 04:36:08

/// <summary>
/// Set Cookies
/// </summary>
/// <param name="iMinutes">Cookie Expired Minutes</param>
/// <returns>true if the cookie was successfully added; false otherwise.</returns>
public static bool SetCookies(string Key, string Value, int iMinutes)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[Key];
if (cookie == null)
{
cookie = new HttpCookie(Key, Value);
//cookie.Domain = "/";
}
else
{
cookie.Value = Value;
}
cookie.Expires = DateTime.Now.AddMinutes(iMinutes);
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
System.Web.HttpContext.Current.Request.Cookies.Add(cookie);
return true;
}

/// <summary>
/// Get Coolies string Value, null if no this cookie.
/// </summary>
/// <param name="Key">Cookie Name</param>
public static string GetCookies(string Key)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[Key];
if (cookie != null)
{
return cookie.Value;
}
else
{
return null;
}
}

登录页面在本地VS里浏览不会出乱码,但发布到iis里就会出中文乱码,搜索了很多这样的情况,但都是没有解决这问题,请大虾帮忙,急~~~~~~~~~~~~
...全文
706 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
lewislxh 2012-08-13
  • 打赏
  • 举报
回复
我和楼主的现象一样~但是用了3楼的方法就解决了~所以楼主是不是别的地方有什么问题
ycproc 2011-04-22
  • 打赏
  • 举报
回复
看看你的网页编码格式

有没有 不统一的地方
liuchangsheng 2011-04-22
  • 打赏
  • 举报
回复
public void SetObj(string strCookieName, int iExpires, string strValue)
{
HttpCookie objCookie = new HttpCookie(strCookieName.Trim());
objCookie.Value = HttpUtility.UrlEncode(strValue.Trim());
objCookie.Domain = ConfigurationManager.AppSettings["Domain"];
if (iExpires > 0)
{
if (iExpires == 1)
{
objCookie.Expires = DateTime.MaxValue;
}
else
{
objCookie.Expires = DateTime.Now.AddSeconds(iExpires);
}
}
HttpContext.Current.Response.Cookies.Add(objCookie);
}


public string GetValue(string strCookieName, string strKeyName)
{
if (HttpContext.Current.Request.Cookies[strCookieName] == null)
{
return "CookieNonexistence";
}
else
{
string strObjValue = HttpContext.Current.Request.Cookies[strCookieName].Value;
string strKeyName2 = strKeyName + "=";
if (strObjValue.IndexOf(strKeyName2) == -1)
{
return "KeyNonexistence";
}
else
{
return HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[strCookieName][strKeyName]);
}
}
}

楼主,用这个方法,你试一下,应该是可以地
se7en 2011-04-22
  • 打赏
  • 举报
回复
一般来说,多语言的系统整合总是个比较郁闷的事情。
特别是统一登陆,那么如果简单来搞定那么就是用cookie了。

但是这里很快出来一个问题,cookie英文的时候一点问题也没有,特别是我们这些喜欢用test做测试数据的程序员,更是系统发布了以后才被用户告知这样的错误。所以呢,以后历尽用"测试"来进行测试了,顺便支持一下中国字嘛。

这里其实也没有什么太多要说的,只是说一下asp,php,javascript这三个和.net中cookie编码的问题。
在asp,php,javascript里面,cookie保存的时候就进行了urlencode。但是.net里面默认却没有这样的编码。所以一定要注意,如果.net读取这三个cookie的时候,直接进行urldecode 就好了。当然,之前这几个也不需要urlencode,否则.net就要郁闷的decode两次了。
当然这个过程中一定要注意编码统一,gb2312的一定要注意,读取cookie的时候一定要编码转换一下。
yangchun1213 2011-04-22
  • 打赏
  • 举报
回复
楼上等于放了个屁,妈的,我不知道啊,但是用编码和解码还是没解决啊
System.Web.HttpContext.Current.Server.UrlDecode 和 System.Web.HttpContext.Current.Server.UrlEncode
这样能解决吗?
winner2050 2011-04-22
  • 打赏
  • 举报
回复
简单的编码解码问题而已。
yangchun1213 2011-04-22
  • 打赏
  • 举报
回复

看来真没高手了,郁闷
yangchun1213 2011-04-19
  • 打赏
  • 举报
回复
偌大的一个CSDN论坛,竟然无人知晓这个问题,不是吧,我的苍天啊~
yangchun1213 2011-04-19
  • 打赏
  • 举报
回复
System.Web.HttpContext.Current.Server.UrlDecode 和 System.Web.HttpContext.Current.Server.UrlEncode

楼上说的比唱的还好听,大家都知道这个道理,但具体实现啊?就我那段代码,要怎么修改,我已经试过写时编码读时解码,没用啊
tomscat 2011-04-19
  • 打赏
  • 举报
回复
写时编码,读时解码
yangchun1213 2011-04-19
  • 打赏
  • 举报
回复
楼上的方法我已经用过了,我曾经这样改过


/// <summary>
/// Set Cookies
/// </summary>
/// <param name="iMinutes">Cookie Expired Minutes</param>
/// <returns>true if the cookie was successfully added; false otherwise.</returns>
public static bool SetCookies(string Key, string Value, int iMinutes)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[Key];
if (cookie == null)
{
cookie = new HttpCookie(Key, HttpUtility.UrlEncode(Value));
//cookie.Domain = "/";
}
else
{
cookie.Value = HttpUtility.UrlEncode(Value);
}
cookie.Expires = DateTime.Now.AddMinutes(iMinutes);
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
System.Web.HttpContext.Current.Request.Cookies.Add(cookie);
return true;
}

/// <summary>
/// Get Coolies string Value, null if no this cookie.
/// </summary>
/// <param name="Key">Cookie Name</param>
public static string GetCookies(string Key)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[Key];
if (cookie != null)
{
return HttpUtility.UrlDecode(cookie.Value);
}
else
{
return null;
}
}



但没起到作用,难道我上面这段代码有问题?请大侠仔细帮我研究一下我的代码是否有问题?
ajq1989 2011-04-19
  • 打赏
  • 举报
回复
在写入Cookie时,先用Url编码,然后再写入,读取时再解码


HttpCookie cookie=new HttpCookie("Test");
cookie.Values.Add("Test1",HttpUtility.UrlEncode("cookie写入"));
Response.AppendCookie(cookie);


//读取
HttpCookie cookie=Request.Cookies["Test"];
string simple1=HttpUtility.UrlDecode(cookie["Test1"]);
子夜__ 2011-04-19
  • 打赏
  • 举报
回复
设置编码 要一致。

英文应该没问题。
liukaizxc 2011-04-19
  • 打赏
  • 举报
回复
用Session 吧 我也是这种情况 没解决 唉 最后用Session
期望高手给回答

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧