user1922254
user1922254

Reputation: 41

Passing Cookies from Java to Browser

I've been trying to pass cookies from an HttpsURLConnection to my browser. Unfortunately, I haven't found... Well, anything at all on the topic besides Android, which is not what I want. The cookies are session-specific, so I have to download them from the webpage every time. Is there any way to open a webpage from Java in a browser (Firefox, Chrome, etc) and send cookies over?

Code so far: (Yes, I know putting "throws Exception" on the main method is not smart in any way. Please just ignore it, it won't be there when this is working.)

  public static void main(String[] args) throws Exception {
    String httpsURL = "https://www.link.com";
    URL myurl = new URL(httpsURL);
    HttpsURLConnection con;

    CookieManager cManager = new CookieManager();
    CookieHandler.setDefault(cManager);
    /* Start by connecting to website so CookieManager can grab cookies */
    con = (HttpsURLConnection) myurl.openConnection();
    /*COOKIES*/
    CookieStore cookieJar = cManager.getCookieStore();
    List<HttpCookie> cookies = cookieJar.getCookies();
    System.out.println("COOKIES:");
    String list = null;
    for (HttpCookie cookie : cookies) {
      if (list != null) {
        list += "; ";
      }
      list += cookie.getName()+"="+cookie.getValue();
      System.out.println(cookie.getName() + " : " + cookie.getValue());
    }
    con.disconnect();
    // Here is where I want the cookies to transfer to the browser...
  }

Upvotes: 0

Views: 2333

Answers (1)

user1922254
user1922254

Reputation: 41

SOLVED:

I'm using Firefox, so I had the program access the Firefox cookies database using SQLite and add/modify the cookies manually. Something like this:

  public static void writeCookie(List<String> nameList, List<HttpCookie> cookies, int lastID) {
    Connection connection;
    Statement statement;
    try {
      Class.forName("org.sqlite.JDBC");
      connection = DriverManager.getConnection("jdbc:sqlite:" + cookieDirectory + "cookies.sqlite");
      statement = connection.createStatement();
      connection.setAutoCommit(false);
      System.out.println.println("~~~~~~~~~~~~~~~~~Opened database successfully~~~~~~~~~~~~~~~~~~~~");
      HttpCookie myCookie;
      for (int a = 0; a < cookies.size(); a++) {
        System.out.println("=========Cookie " + a + "...===========");
        myCookie = cookies.get(a);
        System.out.println("Name = " + myCookie.getName());
        System.out.println("Value = " + myCookie.getValue());
        System.out.println("Max Age = " + myCookie.getMaxAge());
        System.out.println("Comment = " + myCookie.getComment());
        System.out.println("Path = " + myCookie.getPath());
        if (nameList.contains(myCookie.getName())) { // UPDATE COOKIE
          System.out.println("Updating");
          String sql = "UPDATE moz_cookies set value = '" + myCookie.getValue() + "' where name='" + myCookie.getName() + "';";
          statement.executeUpdate(sql);
          connection.commit();
        } else { // CREATE NEW COOKIES
          System.out.println("Creating " + myCookie.getName());
          System.out.println("id = " + lastID);
          String sql = "INSERT INTO moz_cookies (ID,BASEDOMAIN,APPID,INBROWSERELEMENT,NAME,VALUE,HOST,PATH,EXPIRY,LASTACCESSED,CREATIONTIME,ISSECURE) "
                  + "VALUES (" + lastID + ", 'site.com', 0, 0, '" + myCookie.getName() + "', '" + myCookie.getValue() + "', '.site.com', '" + myCookie.getPath()
                  + "', 1464970835, " + (System.currentTimeMillis() * 1000) + ", " + (System.currentTimeMillis() * 1000) + ", '" + myCookie.getSecure() + "' );";
          statement.executeUpdate(sql);
          connection.commit();
          lastID++;
        }
      }
      connection.commit();
      connection.close();
      statement.close();
      System.out.println("Cookies successfully saved!");
    } catch (Exception e) {
      System.out.println(e.getMessage());
      e.printStackTrace();
    }
  }

Upvotes: 2

Related Questions