using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions; namespace DotNet.Utilities
{
/// <summary>
/// Cookie操作帮助类
/// </summary>
public static class HttpCookieHelper
{
/// <summary>
/// 根据字符生成Cookie列表
/// </summary>
/// <param name="cookie">Cookie字符串</param>
/// <returns></returns>
public static List<CookieItem> GetCookieList(string cookie)
{
List<CookieItem> cookielist = new List<CookieItem>();
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[].Value, Value = m.Groups[].Value });
}
}
return cookielist;
} /// <summary>
/// 根据Key值得到Cookie值,Key不区分大小写
/// </summary>
/// <param name="Key">key</param>
/// <param name="cookie">字符串Cookie</param>
/// <returns></returns>
public static string GetCookieValue(string Key, string cookie)
{
foreach (CookieItem item in GetCookieList(cookie))
{
if (item.Key == Key)
return item.Value;
}
return "";
}
/// <summary>
/// 格式化Cookie为标准格式
/// </summary>
/// <param name="key">Key值</param>
/// <param name="value">Value值</param>
/// <returns></returns>
public static string CookieFormat(string key, string value)
{
return string.Format("{0}={1};", key, value);
}
} /// <summary>
/// Cookie对象
/// </summary>
public class CookieItem
{
/// <summary>
/// 键
/// </summary>
public string Key { get; set; }
/// <summary>
/// 值
/// </summary>
public string Value { get; set; }
}
}

最新文章

  1. java 创建线程的三种方法Callable,Runnable,Thread比较及用法
  2. JVM学习笔记:虚拟机的类加载机制
  3. 过滤掉combobox里名称相同的选项
  4. 导出EXCEL【Web方式HTML通过拼接html中table】
  5. svn提交时强制添加注释 (转)
  6. 刷新本地的DNS缓存
  7. JAVA获取当前系统时间System.currentTimeMillis()
  8. css笔记14:css文件之间可以相互引用
  9. ios 数字禁止变成电话号码
  10. js操作json添加元素和数据的方法
  11. 【转】android camera(一):camera模组CMM介绍
  12. 开发框架(OrchardNoCMS)介绍(一)
  13. 【转】http://www.cnblogs.com/yuzukwok/p/3884377.html
  14. px和sp什么区别
  15. OC类的介绍
  16. LoadRunner性能测试-loadrunner事务
  17. Python datetime之timedelta
  18. hive分区(partition)
  19. 什么是内部类? Static Nested Class 和 Inner Class 的不同。
  20. cxGrid 的 Sorting和Filtering的总开关

热门文章

  1. microkernel architecture - Proxy
  2. 【2015 Week Task】
  3. Sharepoint学习笔记—修改SharePoint的Timeouts (Execution Timeout)
  4. Siebel Tools 开发笔记
  5. how find a record import other database.
  6. Exchange 2016中的削减内容
  7. java之大文件断点续传
  8. 失去光标display=none事件的坑
  9. 确定浏览器是否支持某些DOM模块
  10. OC static 和变量