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[].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

c# winform 火狐浏览器 查看cookie的更多相关文章

  1. 浏览器查看cookie

    今天总结下,教你怎样查看一些浏览器的Cookie,比如IE.Firefox.Chrome的Cookies等.下面分块介绍,以后会关注一些没有讲到的浏览器获取Cookie的方法. 1.Firefox浏览 ...

  2. 怎么清除火狐浏览器的cookie?

    火狐浏览器清除Cookie方法/步骤 1.打开火狐浏览器.并在火狐浏览器工具栏找到并单击“工具”下的“选项”. 2.在打开的“火狐浏览器选项”程序窗口中,找到工具栏中的“隐私”并单击,在隐私选项下找到 ...

  3. 关于火狐浏览器设置cookie的一个问题

    最近发现我一个项目的网页,里面的cookie无法添加了,急的我瞪着我的PHP代码沉思了好久,我默认用的火狐浏览器,然而我默默的打开另一个叫360的浏览器,发现它的cookie是正常添加的. ... 难 ...

  4. Chrome浏览器查看cookie

    原文:http://jingyan.baidu.com/article/6b18230954dbc0ba59e15960.html 1. 查看页面的cookie 方法: a). 点击地址栏前面的文档薄 ...

  5. 谷歌浏览器和火狐浏览器如何查看HTTP协议

    谷歌浏览器和火狐浏览器如何查看HTTP协议 谷歌浏览器查看HTTP协议 火狐浏览器查看HTTP协议

  6. 删除浏览器浏览器删除cookie方法

    上班之余抽点时光出来写写博文,希望对新接触的朋友有帮助.今天在这里和大家一起学习一下删除浏览器 文章目录导航 适用范围及演示工具 什么是cookie? cookie有什么作用? ie6/ie7/ie8 ...

  7. 火狐浏览器Firefox 如何下载网页的SWF视频,硅谷动力的网站视频怎么下载

    1 使用火狐浏览器查看到底视频在哪里,我随便开了一段视频,发现这个SWF(外框套了一个Control.swf,内层才是真实的09-class.swf)   2 我们从下面这一段代码中进行分析 < ...

  8. 我给女朋友讲编程CSS系列(3) CSS如何设置字体的类型、大小、颜色,如何使用火狐浏览器的Firebug插件查看网页的字体

    一.CSS如何设置字体的类型.大小.颜色 设计网页时,一般设置body的字体,让其他标签继承body的字体,这样设置特别方便,但是标题标签h1到h6和表单标签(input类型)是没有继承body的字体 ...

  9. 转:Chrome浏览器查看网站登录 Cookie 信息的方法

    当我们使用自动签到等程序的时候一般都要用到网站Cookie,我们可以借助浏览器的扩展来获取Cookie信息,但其实通过浏览器本身的功能就可以查看Cookie信息.以Chrome类浏览器为例有以下三种方 ...

随机推荐

  1. 内存流和null字节

    #include <stdio.h> #include <string.h> int main() { ]={}; FILE* fp = fmemopen(buf,," ...

  2. ubuntu更新删除旧内核的shell脚本

    ubuntu经常提示要更新内核,更新几次后 /boot目录就满了,再更新就提示目录没空间了,这时候就需要删除不用的老旧内核,之前都是uname, grep, dpkg之类的命令一条条敲,然后用眼睛看需 ...

  3. java并发编程实践笔记

    文章转自:http://kenwublog.com/java-concurrency-in-practise-note 1, 保证线程安全的三种方法 :a, 不要跨线程访问共享变量b, 使共享变量是 ...

  4. 【C#】SQL数据库助手类2.0(自用)

    using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...

  5. 微信公众号开发之LBS

    百度地图Web服务api:http://lbsyun.baidu.com/index.php?title=webapi 1.测距 Route Matrix API v2.0:http://lbsyun ...

  6. HDU 4941 Magical Forest --STL Map应用

    题意: 有n*m个格子(n,m <= 2*10^9),有k(k<=10^5)个格子中有值,现在有三种操作,第一种为交换两行,第二种为交换两列,交换时只有两行或两列都有格子有值或都没有格子有 ...

  7. vector中pair的排序方法

    直接上代码: bool judge(const pair<int,char> a, const pair<int ,char> b) { return a.first<b ...

  8. jquery判断div滚动条到底部

    jQuery 里和滚动条有关的概念很多,但是有三个属性和滚动条的拖动有关,就是:scrollTop.scrollLeft.scrollHeight.其中 scrollHeight 属性,互联网上几乎搜 ...

  9. C# Thread.Join()用法的理解 转

    指在一线程里面调用另一线程join方法时,表示将本线程阻塞直至另一线程终止时再执行 比如 1using System; 2 3namespace TestThreadJoin 4{ 5 class P ...

  10. f2fs解析(六)

    f2fs中有对一个bitmap进行操作的函数,感觉很巧妙,和大家分享一下: 1333 static inline void f2fs_change_bit(unsigned int nr, char ...