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