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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM