. Net/C # the application directly reads the local Cookies (because WinXP SP2 calls API: InternetGetCookie)

Source: Internet
Author: User

. Net/C # the application directly reads the local Cookies (WinXP SP2 calls API: InternetGetCookie)
Using System;
Using System. IO;
Using System. Text;
Using System. Text. RegularExpressions;
Using Microsoft. Win32;
Public class Class1
{
Static void Main (string [] args)
{
System. Console. WriteLine (GetCookiesFromFiles ("et8"); // supports WinXP SP2
System. Console. WriteLine (GetCookie ("http://bbs.et8.net "));
System. Console. ReadLine ();
}

[System. Runtime. InteropServices. DllImport ("wininet. dll", CharSet = System. Runtime. InteropServices. CharSet. Auto, SetLastError = true)]
Public static extern bool InternetGetCookie (string lpszUrlName, string lpszCookieName, StringBuilder lpszCookieData, ref int lpdwSize );

[System. Runtime. InteropServices. DllImport ("kernel32.dll")]
Internal static extern Int32 GetLastError ();

Public static string GetCookie (string url) // Win32 API
{
Int size = 1000;
StringBuilder sb = new StringBuilder (size );
If (! InternetGetCookie (url, "", sb, ref size ))
{
Console. WriteLine ("Error code: {0}", GetLastError ());
}
Return sb. ToString ();
}

Public static string GetCookiesFromFiles (string MasterDomain) // Cookies File
{
String S = null;
RegistryKey key = Registry. CurrentUser. OpenSubKey (@ "Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders", false );
If (key! = Null)
{
String val = null;
// Foreach (string s in key. GetValueNames ())
//{
// If (s. ToUpper () = "COOKIES ")
//{
// Val = (string) key. GetValue (s );
// Break;
//}
//}
Val = (string) key. GetValue ("cooKies ");
Val = System. Environment. GetFolderPath (System. Environment. SpecialFolder. Cookies );
If (val! = Null)
{
System. Console. WriteLine ("user Cookies file directory:" + val );
String [] F = Directory. GetFiles (val );
String s = null;
Int I;
Regex r = new Regex (@ ". * @" + MasterDomain + @ "(/[/d +/])? /. Txt ");

For (I = 0; I <F. Length; I ++)
{
If (r. IsMatch (F [I])
{
S = F [I];
System. Console. WriteLine ("user Cookies:" + s );
}
}
If (s! = Null) // s is the latest file
{
StreamReader sr = new StreamReader (s );
S = null;
I = 1;
While (s = sr. ReadLine ())! = Null)
{
If (s = "*" | s = "/n ")
{
I = 0;
}
// Read-only two lines per section
If (I = 1)
{
S + = s;
}
Else if (I = 2)
{
S + = "=" + s + ";";
}
I ++;
}
}
}
}
Return S;
}
}

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.