C # winform Firefox browser view cookies

Source: Internet
Author: User

C # winform Firefox browser viewing cookies

Cookie data for Firefox is located in:%appdata%\mozilla\firefox\profiles\ The Xxx.default directory in the directory, a file named Cookies.sqlite.
such as: C:\Users\jay\AppData\Roaming\Mozilla\Firefox\Profiles\ji4grfex.default\cookies.sqlite
To view cookies in Firefox, you can select Tools > Options > Privacy > Show cookies.

1, get cookies.sqlite file path

2, SQLite database requires reference System.Data.SQLite
 :/http System.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
  Get System.Data.SQLite.dll added to project reference;

* You can use SQLite Database browser to view data:
 : http://sourceforge.net/projects/sqlitebrowser/

3, SQLite.Interop.dll file to be copied to the output directory

4, return value by name and Host field query, value with Chinese to translate
  Httputility.urldecode need to reference system.web

Well, SQLite is really powerful, but it's not very cool here, because it takes the above 2 DLL files to work properly.
There is another way to achieve this, although efficiency is not high, but it is enough.

First Use Notepad to open the Cookies.sqlite file watch
Get format:
  site (domain name) +cookie name + content + domain + path

Implementation by specifying the name + domain, The middle part is the cookie value. The
uses regular expressions:
(? <=jd.com_pst) (? '). Value ' [\w%]+] (? =.jd.com)

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Data.SQLite;usingSystem.Drawing;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Text.RegularExpressions;usingSystem.Windows.Forms;namespacefirefoxcookie{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private stringDbPath =string. Empty;//cookies.sqlite file path        Private voidForm1_Load (Objectsender, EventArgs e) {DirectoryInfo di=NewDirectoryInfo (System.Environment.GetEnvironmentVariable ("AppData") +@"\mozilla\firefox\profiles\"); Directoryinfo[] Dirs= di. GetDirectories ();//get a list of sub-folders            if(Dirs! =NULL) {DbPath= dirs[0]. FullName +"\\cookies.sqlite"; }            //All Cookies             This. Datagridview1.datasource = GetTable ("SELECT * from Moz_cookies"); //All Sites             This. Combobox1.datasource = GetTable ("Select Distinct basedomain from Moz_cookies");  This. Combobox1.displaymember ="Basedomain"; //trigger When registering event property value changesCombobox1.selectedindexchanged + =NewSystem.EventHandler ( This.                    SelectedIndexChanged); }        //find by specifying a site        Private voidSelectedIndexChanged (Objectsender, EventArgs e) {            stringTXT =((ComboBox) sender).            Text;  This. Datagridview1.datasource = GetTable ("SELECT * from moz_cookies where [email protected]",NewSqliteparameter ("Basedomain", TXT)); }        Private voidButton1_Click (Objectsender, EventArgs e) {            stringName = This. TxtName.Text.Trim (); stringHost = This. TxtHost.Text.Trim (); stringValue =string.            Empty; if(File.exists (DbPath)) {stringSqlstr ="Select value from moz_cookies where [email protected] and [email protected]"; Sqliteparameter[] PMS=Newsqliteparameter[]{NewSqliteparameter ("name", name),NewSqliteparameter ("Host", host)}; Value= (string) ExecuteScalar (SQLSTR, PMS)??"not Found! "; } Value= System.Web.HttpUtility.UrlDecode (value);//Chinese requires transcoding             This. Txtvalue.text =value; }        Private ObjectExecuteScalar (stringSqlparamssqliteparameter[] PMS) {                        using(Sqliteconnection conn =NewSqliteconnection ("Data Source ="+DbPath)) {                using(Sqlitecommand com =Newsqlitecommand (SQL, conn)) {                    if(PMS! =NULL) {com.                    Parameters.addrange (PMS); } conn.                    Open (); returncom.                ExecuteScalar (); }            }        }        PrivateDataTable GetTable (stringSqlparamssqliteparameter[] PMS) {            using(Sqliteconnection conn =NewSqliteconnection ("Data Source ="+DbPath)) {                using(Sqlitedataadapter adapter =Newsqlitedataadapter (SQL, conn)) {DataTable dt=NewDataTable (); if(PMS! =NULL) {adapter.                    SelectCommand.Parameters.AddRange (PMS); } conn.                    Open (); Adapter.                    Fill (DT); returnDT; }            }        }        Private voidButton2_Click (Objectsender, EventArgs e) {            stringstr =string.            Empty; using(FileStream fs =NewFileStream (DbPath, FileMode.Open, FileAccess.Read, Fileshare.readwrite)) {                using(StreamReader sr =NewStreamReader (FS, System.Text.Encoding.Default)) {STR=Sr.                ReadToEnd (); }            }                        stringPattern =string. Format (@"(? <={0}) (? ') Value ' [\w%]+] (? ={1})", This. TxtName.Text.Trim (), This. TxtHost.Text.Trim ()); Match Match=Regex.match (str, pattern); if(match. Success) {stringValue = match. groups["value"].                Value; Value= System.Web.HttpUtility.UrlDecode (value);//Chinese requires transcoding                 This. Txtvalue.text =value; }        }    }}

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

C # winform Firefox browser view cookies

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.