Hola,
el siguiente código sirve para obtener las cookies de Firefox (versiones 3+).

Antes que nada hay que saber que sólo obtiene las cookies del Profile0 (en la mayoría de casos sólo existe este perfil).

Primero hay que usar la clase IniFile (aunque se pueden usar homólogos) para manejar los archivos .ini sin usar librerías. Luego hay que usar el driver JDBC de SqLite (buscad en Google ).

IniFile.java

Código: Seleccionar todo


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IniFile {

   private Pattern  _section  = Pattern.compile( "\\s*\\[([^]]*)\\]\\s*" );
   private Pattern  _keyValue = Pattern.compile( "\\s*([^=]*)=(.*)" );
   private Map< String,
      Map< String,
         String >>  _entries  = new HashMap<>();

   public IniFile( String path ) throws IOException {
      load( path );
   }

   public void load( String path ) throws IOException {
      try( BufferedReader br = new BufferedReader( new FileReader( path ))) {
         String line;
         String section = null;
         while(( line = br.readLine()) != null ) {
            Matcher m = _section.matcher( line );
            if( m.matches()) {
               section = m.group( 1 ).trim();
            }
            else if( section != null ) {
               m = _keyValue.matcher( line );
               if( m.matches()) {
                  String key   = m.group( 1 ).trim();
                  String value = m.group( 2 ).trim();
                  Map< String, String > kv = _entries.get( section );
                  if( kv == null ) {
                     _entries.put( section, kv = new HashMap<>());   
                  }
                  kv.put( key, value );
               }
            }
         }
      }
   }

   public String getString( String section, String key, String defaultvalue ) {
      Map< String, String > kv = _entries.get( section );
      if( kv == null ) {
         return defaultvalue;
      }
      return kv.get( key );
   }

   public int getInt( String section, String key, int defaultvalue ) {
      Map< String, String > kv = _entries.get( section );
      if( kv == null ) {
         return defaultvalue;
      }
      return Integer.parseInt( kv.get( key ));
   }

   public float getFloat( String section, String key, float defaultvalue ) {
      Map< String, String > kv = _entries.get( section );
      if( kv == null ) {
         return defaultvalue;
      }
      return Float.parseFloat( kv.get( key ));
   }

   public double getDouble( String section, String key, double defaultvalue ) {
      Map< String, String > kv = _entries.get( section );
      if( kv == null ) {
         return defaultvalue;
      }
      return Double.parseDouble( kv.get( key ));
   }
}
Ahora son dos métodos, el primero para obtener el nombre del perfil y el otro para obtener las cookies.

Obtener el nombre del perfil (para acceder a la carpeta)

Código: Seleccionar todo

public static String getFirefoxPath() {
        String r = "";
        try {
            if (Library.isWindows()) {
                String prof = new IniFile(System.getenv("APPDATA") + "/Mozilla/Firefox/profiles.ini").getString("Profile0", "Path", "");
                r = System.getenv("APPDATA")+"\\Mozilla\\Firefox\\Profiles\\" + prof.split("/")[1];
            }
        } catch (Exception ex) {
            Library.showError(ex);
        }
        return r;
    }
Obtener las cookies

Código: Seleccionar todo

public static ArrayList<String> getCookies() {
        ArrayList<String> r = new ArrayList<>();
        Connection connection = null;
        ResultSet resultSet = null;
        Statement statement = null;
        try {
            Class.forName("org.sqlite.JDBC");
            connection = DriverManager.getConnection("jdbc:sqlite:"+getFirefoxPath()+"\\cookies.sqlite");
            statement = connection.createStatement();
            resultSet = statement.executeQuery("SELECT * FROM moz_cookies");

            while (resultSet.next()) {
                String key = resultSet.getString("baseDomain");
                String value = resultSet.getString("value");
                String name = resultSet.getString("name");
                System.out.println("# " + key + "\n\t- Name: " + name + "\n\t- Value: " + value);
            }
        } catch (Exception ex) {
            Library.showError(ex);
        }
        return r;
    }
Antes de correr tienes que aprender a caminar.

Aprende a programar un poco en Java y no te costará nada aplicar estos métodos.
Responder

Volver a “Fuentes”