您的位置:首页 > 编程语言 > C#

[转].Net/C# 应用程序直接读取本地 Cookies 文件

2011-08-31 16:09 411 查看
.net/C#应用程序直接读取本地Cookies文件(WinXPSP2调用API:InternetGetCookie)

usingSystem;
usingSystem.IO;
usingSystem.Text;
usingSystem.Text.RegularExpressions;
usingMicrosoft.Win32;
publicclassClass1
{
staticvoidMain(string[]args)
{
System.Console.WriteLine(GetCookiesFromFiles("et8"));//支持WinXPSP2
System.Console.WriteLine(GetCookie("http://bbs.et8.net"));
}
[System.Runtime.InteropServices.DllImport("wininet.dll",CharSet=System.Runtime.InteropServices.CharSet.Auto,SetLastError=true)]
publicstaticexternboolInternetGetCookie(stringlpszUrlName,stringlpszCookieName,StringBuilderlpszCookieData,refintlpdwSize);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
internalstaticexternInt32GetLastError();
publicstaticstringGetCookie(stringurl)//Win32API
{
intsize=1000;
StringBuildersb=newStringBuilder(size);
if(!InternetGetCookie(url,"",sb,refsize))
{
Console.WriteLine("Errorcode:{0}",GetLastError());
}
returnsb.ToString();
}
publicstaticstringGetCookiesFromFiles(stringMasterDomain)//CookiesFile
{
stringS=null;
ReGIStryKeykey=Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\ShellFolders",false);
if(key!=null)
{
stringval=(string)key.GetValue("Cookies");
if(val!=null)
{
string[]F=Directory.GetFiles(val);
strings=null;
inti;
Regexr=newRegex(@".*@" MasterDomain @"*\[\d \].txt");
for(i=0;i<F.Length;i )
{
if(r.IsMatch(F[i]))
{
s=F[i];
}
}
if(s!=null)//s就是最新文件
{
StreamReadersr=newStreamReader(s);
s=null;
i=1;
while((s=sr.ReadLine())!=null)
{
if(s=="*"||s=="\n")
{
i=0;
}
//每节只读两行
if(i==1)
{
S =s;
}
elseif(i==2)
{
S ="=" s ";";
}
i ;
}
}
}
}
returnS;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐