zoukankan      html  css  js  c++  java
  • 一个好用的C# HttpCookieHelper.cs类

     1  using System;  
     2 using System.Collections.Generic;  
     3 using System.Text;  
     4 using System.Text.RegularExpressions;  
     5   
     6 namespace DotNet.Utilities  
     7 {  
     8      /// <summary>  
     9     /// Cookie操作帮助类  
    10     /// </summary>  
    11     public static class HttpCookieHelper  
    12     {  
    13         /// <summary>  
    14         /// 根据字符生成Cookie列表  
    15         /// </summary>  
    16         /// <param name="cookie">Cookie字符串</param>  
    17         /// <returns></returns>  
    18         public static List<CookieItem> GetCookieList(string cookie)  
    19         {  
    20             List<CookieItem> cookielist = new List<CookieItem>();  
    21             foreach (string item in cookie.Split(new string[] { ";", "," }, StringSplitOptions.RemoveEmptyEntries))  
    22             {  
    23                 if (Regex.IsMatch(item, @"([sS]*?)=([sS]*?)$"))  
    24                 {  
    25                     Match m = Regex.Match(item, @"([sS]*?)=([sS]*?)$");  
    26                     cookielist.Add(new CookieItem() { Key = m.Groups[1].Value, Value = m.Groups[2].Value });  
    27                 }  
    28             }  
    29             return cookielist;  
    30         }  
    31   
    32         /// <summary>  
    33         /// 根据Key值得到Cookie值,Key不区分大小写  
    34         /// </summary>  
    35         /// <param name="Key">key</param>  
    36         /// <param name="cookie">字符串Cookie</param>  
    37         /// <returns></returns>  
    38         public static string GetCookieValue(string Key, string cookie)  
    39         {  
    40             foreach (CookieItem item in GetCookieList(cookie))  
    41             {  
    42                 if (item.Key == Key)  
    43                     return item.Value;  
    44             }  
    45             return "";  
    46         }  
    47         /// <summary>  
    48         /// 格式化Cookie为标准格式  
    49         /// </summary>  
    50         /// <param name="key">Key值</param>  
    51         /// <param name="value">Value值</param>  
    52         /// <returns></returns>  
    53         public static string CookieFormat(string key, string value)  
    54         {  
    55             return string.Format("{0}={1};", key, value);  
    56         }  
    57     }  
    58   
    59     /// <summary>  
    60     /// Cookie对象  
    61     /// </summary>  
    62     public class CookieItem  
    63     {  
    64         /// <summary>  
    65         ///66         /// </summary>  
    67         public string Key { get; set; }  
    68         /// <summary>  
    69         ///70         /// </summary>  
    71         public string Value { get; set; }  
    72     }  
    73 }  
  • 相关阅读:
    MongoDB 基础命令行
    AngularJS 监控对象属性:$watch和$digest
    AngularJS核心01:如何启动
    免费的编程中文书籍索引
    AngularJS Intellisense in Visual Studio 2012
    SQL Server中Rowcount与@@Rowcount
    VS 关于 .sln 文件和 .suo 文件
    Dart 基础重点截取 Dart 2 20180417
    io.netty.resolver.dns.DnsNameResolverContext
    mybatis BigDecimal Double Long 的坑爹事
  • 原文地址:https://www.cnblogs.com/anlaoliu/p/7249203.html
Copyright © 2011-2022 走看看