返回顶部

收藏

操作Cookie示例

更多
public class BaiduCookie
    {
        public  CookieContainer mycookie = new CookieContainer();//定义cookie容器
        public  Object obj = new Object();
        public  byte[] byt = new byte[1];
        public  void upcookie(CookieCollection cookie)
        {
            for (int i = 0; i < cookie.Count; i++)
            {
                mycookie.Add(cookie[i]);
            }
            obj = mycookie;
            byt = ObjectToBytes(obj);
        }
        /**/
        /// <summary>
        /// 将一个object对象序列化,返回一个byte[]
        /// </summary>
        /// <param name="obj">能序列化的对象</param>
        /// <returns></returns>
        public static byte[] ObjectToBytes(object obj)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(ms, obj);
                return ms.GetBuffer();
            }
        }

        /**/
        /// <summary>
        /// 将一个序列化后的byte[]数组还原
        /// </summary>
        /// <param name="Bytes"></param>
        /// <returns></returns>
        public  object BytesToObject(byte[] Bytes)
        {
            using (MemoryStream ms = new MemoryStream(Bytes))
            {
                IFormatter formatter = new BinaryFormatter();
                return formatter.Deserialize(ms);
            }
        }
        public  CookieContainer getcookie()
        {
            return mycookie;
        }
//该片段来自于http://outofmemory.cn

标签:c#,.net,网络

收藏

0人收藏

支持

0

反对

0

发表评论