Httpcode version
/// <summary> ///merge cookies, cookie2 and Cookie1 merge update return string type cookie/// </summary> /// <param name= "Cookie1" >Old Cookie</param> /// <param name= "Cookie2" >New Cookie</param> /// <returns></returns> Public stringUpdatecookie (stringCookie1,stringcookie2) {StringBuilder SB=NewStringBuilder (); Dictionary<string,string> Diccookie =Newdictionary<string,string>(); //Traverse Cookie1 if(!string. IsNullOrEmpty (Cookie1)) {foreach(stringCookiesinchCookie1. Replace (',',';'). Split (';')) { if(!string. IsNullOrEmpty (cookies) && cookies. IndexOf ('=') >0) { stringKey = Cookie. Split ('=')[0]. Trim (); stringValue = Cookie. Substring (key. Length +1). Trim (); if(Diccookie.containskey (key)) {Diccookie[key]=cookies; } Else{diccookie.add (key, Cookie); } } } } if(!string. IsNullOrEmpty (Cookie2)) {//Traverse Cookie2 foreach(stringCookiesinchCookie2. Replace (',',';'). Split (';')) { if(!string. IsNullOrEmpty (cookies) && cookies. IndexOf ('=') >0) { stringKey = Cookie. Split ('=')[0]. Trim (); stringValue = Cookie. Substring (key. Length +1). Trim (); if(Diccookie.containskey (key)) {Diccookie[key]=cookies; } Else{diccookie.add (key, Cookie); } } } } inti =0; foreach(varIteminchDiccookie) {i++; if(I <diccookie.count) {sb. Append (item. Value+";"); } Else{sb. Append (item. Value); } } returnsb. ToString (); }
Universal Frame Version
/// <summary> ///automatically merge the values of two cookies to return the updated results/// </summary> /// <param name= "Cookie1" >Cookie1</param> /// <param name= "Cookie2" >Cookie2</param> /// <returns>returns the updated cookie</returns> Internal Static stringGetmergecookie (stringCookie1,stringcookie2) { if(string. Isnullorwhitespace (COOKIE1))//The new one is empty. { returnCookie2;//back to the old } if(string. Isnullorwhitespace (Cookie2))//The old one is empty. { returnCookie1;//returns the new} List<string> cookielist =Newlist<string> ();//Results string[] list_1 = Cookie1. ToString (). Split (';'); string[] list_2 = Cookie2. ToString (). Split (';'); foreach(stringIteminchlist_1) { //Exclude Duplicates if(Cookielist. Contains (item))Continue; //Docking Cookie Basic key and value stringCookielist. ADD (string. Format ("{0}", item)); } foreach(stringIteminchlist_2) { //Exclude Duplicates if(Cookielist. Contains (item))Continue; //Docking Cookie Basic key and value stringCookielist. ADD (string. Format ("{0}", item)); } return string. Join (";", cookielist); }
C#cookie Merging Updates