.Net/C# 应用程序直接读取本地 Cookies 文件(WinXP SP2 调用 API: InternetGetCookie 无果)
2009-08-19 17:22
531 查看
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")); //支持 WinXP SP2
System.Console.WriteLine(GetCookie("http://bbs.et8.net/"));
}
[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 = (string) key.GetValue("Cookies");
if (val!= null)
{
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];
}
}
if (s != null) //s 就是最新文件
{
StreamReader sr = new StreamReader(s);
s = null;
i = 1;
while ((s = sr.ReadLine())!= null)
{
if (s == "*" || s == "\n")
{
i = 0;
}
//每节只读两行
if (i == 1)
{
S += s;
}
else if (i == 2)
{
S += "=" + s + "; ";
}
i++;
}
}
}
}
return S;
}
}
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")); //支持 WinXP SP2
System.Console.WriteLine(GetCookie("http://bbs.et8.net/"));
}
[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 = (string) key.GetValue("Cookies");
if (val!= null)
{
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];
}
}
if (s != null) //s 就是最新文件
{
StreamReader sr = new StreamReader(s);
s = null;
i = 1;
while ((s = sr.ReadLine())!= null)
{
if (s == "*" || s == "\n")
{
i = 0;
}
//每节只读两行
if (i == 1)
{
S += s;
}
else if (i == 2)
{
S += "=" + s + "; ";
}
i++;
}
}
}
}
return S;
}
}
相关文章推荐
- .Net/C# 应用程序直接读取本地 Cookies 文件 (由于 WinXP SP2 调用 API: InternetGetCookie 无果)
- .Net/C# 应用程序直接读取本地 Cookies 文件 (由于 WinXP SP2 调用 API: InternetGetCookie 无果)
- .Net/C#应用程序直接读取本地Cookies文件(由于WinXPSP2调用API:InternetGetCookie无果)
- [转].Net/C# 应用程序直接读取本地 Cookies 文件
- Net/C# 应用程序直接读取本地 Cookies 文件
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- .NET(C#) 平台调用:不依赖平台的GetWindowLongPtr和SetWindowLongPtr API
- ET99 采用C#直接调用API的DLL文件
- 用C#编程从数据库中读取图片数据导进Excel文件的方法(如何从数据库中读取保存的文件,直接打开,中间不保存到本地)
- Unity中C# 文件本地读取,本地保存等实例
- C#读取sqlserver 动画flash swf 文件到本地硬盘
- Html5 本地文件读取 API 研究使用过程中的意外发现 - MDN
- [C#] 计算大文件的MD5的两种方式(直接调用方法计算,流计算-适用于大文件)
- C#调用FFMPEG实现桌面录制(视频+音频+生成本地文件)
- REST接口GET方法获取文件保存到本地(C#)
- C# 本地txt文件读取至comboBox下拉列表
- .NET 读取本地文件绑定到GridViewRow
- C#调用exe文件,IIS发布后无法掉用本地exe程序的解决方法
- c#调用api(FindFirstFile,FindNextFile)高效遍历目录文件
- c#通过文件存储 调用cookies的代码