. 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;
}
}