C # code for calling cookies through file storage

Source: Internet
Author: User
Recent research paperpass thesis considers the need to check the login information when detecting system Logon

Save it, but C # does not want PHP. With this function, Cookies cannot be saved and sent to the server, so a piece of code is written.

Show the get method to obtain the source code of the webpage 1 // <summary>

2 // obtain the HTML code of the page
3 /// </Summary>
4 /// <Param name = "url"> address </param>
5 /// <Param name = "Referer"> laiyou </param>
6 // <Param name = "cookies"> cookies </param>
7 /// <returns> </returns>
8 protected string gethtml (string URL, string Referer, string cookiesfile, bool autore) // obtain the source file of the specified address
9 {
10 httpwebrequest getrequest = (httpwebrequest) webrequest. Create (URL );
11 cookiecontainer cookies = NULL;
12 if (! String. isnullorempty (cookiesfile) & file. exists (cookiesfile ))
13 {
14 cookies = getcookiecontainer (cookiesfile );
15
16 getrequest. cookiecontainer = cookies;
17}
18 getrequest. Accept = "*/*";
19 getrequest. useragent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1 ;)";
20 getrequest. keepalive = false;
21 getrequest. allowautoredirect = autore;
22 if (! String. isnullorempty (Referer ))
23 {
24 getrequest. Referer = Referer;
25}
26 try
27 {
28 httpwebresponse getresponse = (httpwebresponse) getrequest. getresponse ();
29 string [] locationulr = getresponse. headers. getvalues ("location ");
30
31 if (cookies! = NULL & getresponse. Cookies. Count> 0)
32 {
33 cookies. Add (getresponse. Cookies );
34 // Save the cookies File
35 savecookiecontainer (cookies, cookiesfile );
36}
37 // if it is switched
38 If (! Autore & locationulr! = NULL & locationulr. length> 0)
39 {
40 getresponse. Close ();
41 return locationulr [0];
42}
43 streamreader readstream = new streamreader (getresponse. getresponsestream (), encoding. utf8 );
44 var thehtml = readstream. readtoend ();
45 readstream. Close (); readstream. Dispose ();
46 getresponse. Close ();
47
48 return thehtml;
49}
50 catch (exception E)
51 {
52 writelog (E. tostring (), ownshttpcontext. server. mappath ("/"));
53 return NULL;
54}
55 finally
56 {
57 getrequest. Abort ();
58}
59}

The getcookiecontainer function reads data from a file.
The cookiecontainer and savecookiecontainer functions save cookiecontainer to the file!

The two functions are as follows // <summary>

/// Traverse cookiecontainer
/// </Summary>
/// <Param name = "cc"> </param>
/// <Returns> </returns>
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;
}
/// <Summary>
/// Save cookiecontainer to a file
/// </Summary>
/// <Param name = "cookies"> cookiecontainer to be written </param>
/// <Param name = "cookiefile"> what file is saved to </param>
Private Static void savecookiecontainer (cookiecontainer cookies, string cookiefile)
{

Stringbuilder sb = new stringbuilder ();
If (cookies = NULL | cookies. Count = 0) {return ;}
// Check whether the directory exists
If (! File. exists (cookiefile ))
{
Fileinfo cookiefileinfo = new fileinfo (cookiefile );
If (! Cookiefileinfo. Directory. exists)
{
Cookiefileinfo. Directory. Create ();
}
}
List <cookie> cclist = getallcookies (cookies );
Foreach (cookie in cclist)
{
SB. appendformat ("{0} |, | {1} |, | {2} |, | {3} |, | {4} \ r \ n", Cookie. name, Cookie. value, Cookie. expires. ticks, Cookie. domain, Cookie. path );
}
File. writealltext (cookiefile, SB. tostring ());
}
/// <Summary>
/// Obtain cookies from a file
/// </Summary>
/// <Param name = "cookiefile"> cookie-saved files </param>
/// <Returns> </returns>
Private Static cookiecontainer getcookiecontainer (string cookiefile)
{
Cookiecontainer returncookiecontainer = new cookiecontainer ();
If (! File. exists (cookiefile ))
{
Return returncookiecontainer;
}
String [] cookiestrs = file. readalllines (cookiefile );

Foreach (string cookiestr in cookiestrs)
{
String [] tempstrs = cookiestr. Split (New String [] {"|, |"}, stringsplitoptions. removeemptyentries );
If (tempstrs. Length <5)
{
Continue;
}
Cookie cookietemp = new cookie ();
Cookietemp. Name = tempstrs [0];
Cookietemp. value = tempstrs [1];
Cookietemp. expires = new datetime (convert. toint64 (tempstrs [2]);
Cookietemp. Domain = tempstrs [3];
Cookietemp. Path = tempstrs [4];
Returncookiecontainer. Add (cookietemp );
}
Return returncookiecontainer;
}

After testing can be used normally, please indicate the source of the load http://www.cnblogs.com/mutuan/

Related Article

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.