using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace DotNet.Utilities
{
///
/// Cookie操作帮助类
///
public static class HttpCookieHelper
{
///
/// 根据字符生成Cookie列表
///
/// Cookie字符串
///
public static List GetCookieList(string cookie)
{
List cookielist = new List();
foreach (string item in cookie.Split(new string[] { ";", "," }, StringSplitOptions.RemoveEmptyEntries))
{
if (Regex.IsMatch(item, @"([\s\S]*?)=([\s\S]*?)$"))
{
Match m = Regex.Match(item, @"([\s\S]*?)=([\s\S]*?)$");
cookielist.Add(new CookieItem() { Key = m.Groups[1].Value, Value = m.Groups[2].Value });
}
}
return cookielist;
}
///
/// 根据Key值得到Cookie值,Key不区分大小写
///
/// key
/// 字符串Cookie
///
public static string GetCookieValue(string Key, string cookie)
{
foreach (CookieItem item in GetCookieList(cookie))
{
if (item.Key == Key)
return item.Value;
}
return "";
}
///
/// 格式化Cookie为标准格式
///
/// Key值
/// Value值
///
public static string CookieFormat(string key, string value)
{
return string.Format("{0}={1};", key, value);
}
}
///
/// Cookie对象
///
public class CookieItem
{
///
/// 键
///
public string Key { get; set; }
///
/// 值
///
public string Value { get; set; }
}
}