A handy C # HttpCookieHelper. cs class,

Source: Internet
Author: User

A handy C # HttpCookieHelper. cs class,

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 operation help class 10 /// </summary> 11 public static class HttpCookieHelper 12 {13 // <summary> 14 // generate the Cookie list based on characters 15 // </summary> 16 // <param name = "cookie"> Cookie string </param> 17 // <returns> </returns> 18 public static List <Cook IeItem> 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, @ "([\ s \ S] *?) = ([\ S \ S] *?) $ ") 24 {25 Match m = Regex. Match (item, @" ([\ s \ S] *?) = ([\ S \ S] *?) $ "); 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 // the Cookie Value is returned based on the Key, key is case insensitive 34 /// </summary> 35 /// <param name = "Key"> key </param> 36 /// <param name = "cookie"> string 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 // format the Cookie in standard format 49 // </summary> 50 // <param name = "key"> Key value </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 object 61 // </summary> 62 public class CookieItem 63 {64 // <summary> 65 /// Key 66 /// </summary> 67 public string Key {get; set;} 68 // <summary> 69 // Value 70 // </summary> 71 public string Value {get; set;} 72} 73}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.