c# winform 火狐浏览器 查看cookie
Firefox的Cookie数据位于:%APPDATA%MozillaFirefoxProfiles 目录中的xxx.default目录,名为cookies.sqlite的文件。
如:C:UsersjayAppDataRoamingMozillaFirefoxProfilesji4grfex.defaultcookies.sqlite
在Firefox中查看cookie, 可以选择”工具 > 选项 >” “隐私 > 显示cookie”。
1、先获取cookies.sqlite文件路径
2、SQLite数据库需要引用System.Data.SQLite
下载地址:http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
得到System.Data.SQLite.dll添加到工程引用;
*可以使用SQLite Database Browser来查看数据:
下载地址:http://sourceforge.net/projects/sqlitebrowser/
3、SQLite.Interop.dll文件要复制到输出目录中
4、通过name和host字段查询返回value值,value带有中文要转译
HttpUtility.UrlDecode需要引用System.Web
好吧,SQLite确实也很强大,只不过在这里并不是很爽,原因是需要以上2个dll文件才可以正常使用。
那下面就用另一种办法实现下,虽然效率没有上面的高,但已足够用了。
首先使用记事本打开cookies.sqlite文件观察
得到格式:
站点(域名)+Cookie名称+内容+域+路径
实现通过指定名称+域,取中间的部分就是cookie值。
使用正则表达式:
(?<=jd.com_pst)(?'value'[w%]+)(?=.jd.com)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SQLite; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Windows.Forms; namespace FirefoxCookie { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private string dbPath = string.Empty; //cookies.sqlite文件路径 private void Form1_Load(object sender, EventArgs e) { DirectoryInfo di = new DirectoryInfo(System.Environment.GetEnvironmentVariable("AppData") + @"MozillaFirefoxProfiles"); DirectoryInfo[] dirs = di.GetDirectories();//获取子文件夹列表 if (dirs != null) { dbPath = dirs[0].FullName + "\cookies.sqlite"; } //所有cookie this.dataGridView1.DataSource = GetTable("select * from moz_cookies"); //所有站点 this.comboBox1.DataSource = GetTable("select Distinct baseDomain from moz_cookies"); this.comboBox1.DisplayMember = "baseDomain"; //注册事件 属性值更改时触发 comboBox1.SelectedIndexChanged += new System.EventHandler(this.SelectedIndexChanged); } //通过指定站点查找 private void SelectedIndexChanged(object sender, EventArgs e) { string txt = ((ComboBox)sender).Text; this.dataGridView1.DataSource = GetTable("select * from moz_cookies where baseDomain=@baseDomain", new SQLiteParameter("baseDomain", txt)); } private void button1_Click(object sender, EventArgs e) { string name = this.txtName.Text.Trim(); string host = this.txtHost.Text.Trim(); string value = string.Empty; if (File.Exists(dbPath)) { string sqlStr = "select value from moz_cookies where name=@name and host=@host"; SQLiteParameter[] pms = new SQLiteParameter[]{ new SQLiteParameter("name", name), new SQLiteParameter("host", host) }; value = (string)ExecuteScalar(sqlStr, pms) ?? "未找到!"; } value = System.Web.HttpUtility.UrlDecode(value); //中文需要转码 this.txtValue.Text = value; } private object ExecuteScalar(string sql, params SQLiteParameter[] pms) { using (SQLiteConnection conn = new SQLiteConnection("Data Source =" + dbPath)) { using (SQLiteCommand com = new SQLiteCommand(sql, conn)) { if (pms != null) { com.Parameters.AddRange(pms); } conn.Open(); return com.ExecuteScalar(); } } } private DataTable GetTable(string sql, params SQLiteParameter[] pms) { using (SQLiteConnection conn = new SQLiteConnection("Data Source =" + dbPath)) { using (SQLiteDataAdapter adapter = new SQLiteDataAdapter(sql, conn)) { DataTable dt = new DataTable(); if (pms != null) { adapter.SelectCommand.Parameters.AddRange(pms); } conn.Open(); adapter.Fill(dt); return dt; } } } private void button2_Click(object sender, EventArgs e) { string str = string.Empty; using (FileStream fs = new FileStream(dbPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default)) { str = sr.ReadToEnd(); } } string pattern = string.Format(@"(?<={0})(?'value'[w%]+)(?={1})", this.txtName.Text.Trim(), this.txtHost.Text.Trim()); Match match = Regex.Match(str, pattern); if (match.Success) { string value = match.Groups["value"].Value; value = System.Web.HttpUtility.UrlDecode(value); //中文需要转码 this.txtValue.Text = value; } } } }
下载地址: