Cookie操作

1、写cookie方法

    #region 登录时写入cookie
public static void LoginCookieSave(string uid, string loginname, string pwd)
{
System.Web.HttpCookie logincookie = new System.Web.HttpCookie("userinfo");
logincookie.Values.Add("uid", uid);
logincookie.Values.Add("loginname", ICore.IChecking.Escape(loginname));
logincookie.Values.Add("pwd", pwd);
logincookie.Expires = DateTime.Now.AddMonths();
logincookie.Domain = "domain.com";
HttpContext.Current.Response.AppendCookie(logincookie);
}
#endregion

2、读取cookie方法

    /// <summary>
/// 获得Cookie的值
/// </summary>
/// <param name="cookieName"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string GetCookieValue(string cookieName, string key)
{
HttpRequest request = HttpContext.Current.Request;
if (request != null)
return GetCookieValue(request.Cookies[cookieName], key);
return "";
} /// <summary>
/// 获得Cookie的子键值
/// </summary>
/// <param name="cookie"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string GetCookieValue(HttpCookie cookie, string key)
{
if (cookie != null)
{
if (!string.IsNullOrEmpty(key) && cookie.HasKeys)
return cookie.Values[key];
else
return cookie.Value;
}
return "";
}
上一篇:用DOS命令来运行Java代码


下一篇:关于java中Static关键字的加强理解