c# winform 火狐浏览器 查看cookie

c# winform 火狐浏览器 查看cookie

Firefox的Cookie数据位于:%APPDATA%\Mozilla\Firefox\Profiles\ 目录中的xxx.default目录,名为cookies.sqlite的文件。
如:C:\Users\jay\AppData\Roaming\Mozilla\Firefox\Profiles\ji4grfex.default\cookies.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") + @"\Mozilla\Firefox\Profiles\");
            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;
            }
        }
    }
}

 

下载地址:

 http://download.csdn.net/detail/h_gxi/9059561

 

转载于:https://www.cnblogs.com/han1982/p/4768244.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值