部分本地 firefox cookies 无法获取

打一壶酱油 2015-09-09 05:12:09

我在 firefox 的选项->隐私 -> 查看 cookies 可以看到 所有 cookies ,但我用以下代码不能拿到所有的cookies,怪了,会隐藏吗
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;
}
}

private void button3_Click(object sender, EventArgs e)
{
string name = "name";
string host = "XXXXx";
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.textBox1.Text = value;
}
}
}
...全文
120 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧