zoukankan      html  css  js  c++  java
  • 序列化后存储Cookie

            /// <summary>
            /// 存储Cookie
            /// </summary>
            /// <param name="name">Coookies名称</param>
            /// <param name="value">对象</param>
            /// <param name="expiredays">Cookie有效天数</param>
            /// <param name="path">Cookie路径</param>
            public static void AppendCookie(string name, object value, int expiredays, params string[] path)
            {
                //获得序列化字符串
                string CookieString = SerializationFunc.SerializeObject(value);

                foreach (string str in path)
                {
                    HttpCookie Cookie = new HttpCookie(name);
                    Cookie.Value = CookieString;
                    Cookie.Expires = DateTime.Today.AddDays(expiredays);
                    if (str.Trim().Length>0)
                    {
                        Cookie.Path = str;
                    }

                    HttpContext.Current.Response.Cookies.Add(Cookie);
                }
            }

            /// <summary>
            /// 序列化对象
            /// </summary>
            /// <param name="value">对象</param>
            /// <returns>序列化字符串</returns>
            public static string SerializeObject(object value)
            {
                string ReturnString = string.Empty;

                using (MemoryStream SerializationStream = new MemoryStream())
                {
                    BinaryFormatter BFormatter = new BinaryFormatter();
                    BFormatter.Serialize(SerializationStream, value);
                    ReturnString = Convert.ToBase64String(SerializationStream.ToArray());
                }

                return ReturnString;
            }

            /// <summary>
            /// 反序列化对象
            /// </summary>
            /// <param name="value">序列化字符串</param>
            /// <returns>对象</returns>
            public static object DeSerializeObject(string value)
            {
                object ReturnObject;

                using (MemoryStream SerialzationStream = new MemoryStream(Convert.FromBase64String(value)))
                {
                    BinaryFormatter BFormatter = new BinaryFormatter();
                    ReturnObject = (object)BFormatter.Deserialize(SerialzationStream);
                }
                return ReturnObject;
            }

  • 相关阅读:
    Eclipse导入Ant项目
    Eclipse修改默认包路径的起始文件夹
    Java中DAO/DTO/PO/VO/BO/QO/POJO
    FreeMarker与Spring MVC 4集合的HelloWorld示例
    FreeMarker与Spring MVC 4结合错误:Caused by: java.lang.NoClassDefFoundError: org/springframework/ui/freemarker/FreeMarkerConfiguration
    FreeMarker与Servlet结合示例
    FreeMarker-简单示例
    Java模板引擎-FreeMarker
    SiteMesh2-sitemesh.xml的其它映射器的用法
    SiteMesh2-sitemesh.xml的ParameterDecoratorMapper映射器的用法
  • 原文地址:https://www.cnblogs.com/zhouyunbaosujina/p/3127091.html
Copyright © 2011-2022 走看看