C # traverse all cookiecontainer cookies and save them to files

Source: Internet
Author: User
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 );

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.