封装对Cookie和Session设置或取值的类

public class CookieHelper : System.Web.SessionState.IReadOnlySessionState
    {

public static void SetCookies(HttpContext context, string cookieName, Hashtable hashtable)
        {
            HttpCookie myCookie  = new HttpCookie(cookieName);

foreach (DictionaryEntry de in hashtable)
            {
                myCookie.Values.Remove(de.Key.ToString());
                myCookie.Values.Add(de.Key.ToString(), de.Value.ToString());
            }
            context.Response.Cookies.Add(myCookie);
        }

public static void SetSession(HttpContext context, string sessionName, string UserInfoId)
        {
            context.Session[sessionName] = UserInfoId;
        }

public static string GetSession(HttpContext context, string sessionName)
        {
            try
            {
                if (context.Session != null)
                {
                    return context.Session[sessionName].ToString();
                }
                else
                {
                    return "null";
                }
            }
            catch (Exception)
            {

return "null";
            }
        }

public static void RemoveSession(HttpContext context, string sessionName)
        {
            if (context.Session != null)
            {
                context.Session.Remove(sessionName);
            }
        }

public static HttpCookie GetCookies(HttpContext context, string cookieName)
        {
            if (context.Request.Cookies[cookieName] != null)
            {
                HttpCookie myCookie = context.Request.Cookies[cookieName];
                return myCookie;
            }
            else
            {
                return null;
            }
        }

public static void SetCookie(HttpContext context,string cookieName, string cookieValue)
        {
            HttpCookie myCookie = new HttpCookie(cookieName);

myCookie.Value = context.Server.UrlEncode(cookieValue);

context.Response.Cookies.Add(myCookie);
        }

public static string GetCookie(string cookieName)
        {
            HttpContext context = HttpContext.Current;
            if (context.Request.Cookies[cookieName] != null)
                return context.Server.UrlDecode(context.Request.Cookies[cookieName].Value);
            else
                return string.Empty;
        }

public static void RemoveCookie(HttpContext context,string cookieName)
        {
            if (context.Request.Cookies[cookieName] != null)
            {
                SetCookie(context, cookieName, "");
            }
        }

}

上一篇:dubbo-源码分析Provider


下一篇:ural 1246. Tethered Dog