C # traverse all cookiecontainer cookies and save them to files
When you open the HTTP Method to submit data, some users need to log on to the system to send data. If you log on to the system each time, it will be a waste of time. Due to network problems, it takes a lot of time to transmit data back and forth. If you save cookiecontainer to a file or database after logging on, it will be much faster.
Two methods are used to save cookiecontainer. One is to serialize cookiecontainer, save it, And deserialize it when used. The other is to traverse all cookies and read the content, create a new cookie and add it to cookiecontainer. After consideration, we decided to use the second one because the cookie has a time limit. If the cookie is saved after serialization, it may expire, and the other one can set its own time.
The followingCodeIt can be used in many occasions, including forums,Community, Or even some mailboxes. Of course, here I am just an example to save the content to a text file, but it can actually be saved to a database or elsewhere.
First, we use a method to read all cookiecontainer cookies. The code of this method is as follows:
Public static list <cookie> getallcookies (cookiecontainer CC)
{
List <cookie> lstcookies = new list <cookie> ();
Hashtable table = (hashtable) CC. GetType (). invokemember ("m_domaintable ",
System. reflection. bindingflags. nonpublic | system. reflection. bindingflags. getfield |
System. reflection. bindingflags. instance, null, CC, new object [] {});
Foreach (Object pathlist in table. values)
{
Sortedlist lstcookiecol = (sortedlist) pathlist. GetType (). invokemember ("m_list ",
System. reflection. bindingflags. nonpublic | system. reflection. bindingflags. getfield
| System. reflection. bindingflags. instance, null, pathlist, new object [] {});
Foreach (cookiecollection colcookies in lstcookiecol. values)
Foreach (cookie C in colcookies) lstcookies. Add (C );
}
Return lstcookies;
}
Then we save it to the file:
Stringbuilder SBC = new stringbuilder ();
List <cookie> cooklist = code. progtool. getallcookies (cookiecontainer );
Foreach (cookie in cooklist)
{
SBC. appendformat ("{0 };{ 1 };{ 2 };{ 3 };{ 4 };{ 5} \ r \ n ",
Cookie. domain, Cookie. Name, Cookie. Path, Cookie. Port,
Cookie. Secure. tostring (), Cookie. value );
}
Filestream FS = file. Create ("d :\\ chinarencookies.txt ");
FS. Close ();
File. writealltext ("d :\\ chinarencookies.txt", SBC. tostring (), system. Text. encoding. Default );
Read all cookies
String [] cookies = file. readalltext ("d :\\ chinarencookies.txt", system. text. encoding. default ). split ("\ r \ n ". tochararray (), stringsplitoptions. removeemptyentries );
Foreach (string C in cookies)
{
String [] cc = C. Split (";". tochararray ());
Cookie ck = new cookie ();;
CK. Discard = false;
CK. Domain = Cc [0];
CK. Expired = true;
CK. HTTPOnly = true;
CK. Name = Cc [1];
CK. Path = Cc [2];
CK. Port = Cc [3];
CK. Secure = bool. parse (CC [4]);
CK. value = Cc [5];
Cookiecontainer. Add (CK );
}