获取 Cookie

/// <summary>
    /// 获取WPF url 地址中的Cookies
    /// </summary>
    public partial class CookieHelper
    {

#region 获取所有的Cookies 数组
        /// <summary>
        /// 获取所有的Cookies 数组
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetCookies(Uri uri)
        {
            Dictionary<string, string> r_dictionary = new Dictionary<string, string>();
            string _str_cookies = Application.GetCookie(uri);
            string[] _cookiesArry = _str_cookies.Split(' ');
            foreach (var item in _cookiesArry)
            {
                string _temp = item.Trim(';');
                string[] _dictionary = _temp.Split('=');
                r_dictionary.Add(_dictionary[0],_dictionary[1]);
            }
            return r_dictionary;
        }
        #endregion

/// <summary>
        /// 设置Cookies
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="expirationDate"></param>
        public static void SetCookie(Uri uri, String name, String value, DateTime expirationDate)
        {
            String cookie = String.Format("{0}={1}; expires={2}", name, value, GetExpirationDateString(expirationDate));
            Application.SetCookie(uri, cookie);
        }

/// <summary>
        /// 获取Cookies
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static String GetCookie(Uri uri)
        {
            return Application.GetCookie(uri);
        }

private static String GetExpirationDateString(DateTime expirationDate)
        {
            return expirationDate.ToString("ddd, dd-MMM-yyyy HH:mm:ss") + " GMT";
        }

上一篇:Spring mvc配置Json返回


下一篇:【重走Android之路】【路线篇(二)】知识点归纳