将Cookie从Java传递到浏览器

我一直在尝试将HttpsURLConnection中的cookie传递给我的浏览器。 不幸的是,我还没有找到…嗯,除了Android之外的任何关于这个主题的东西,这不是我想要的。 Cookie是特定于会话的,因此我每次都必须从网页下载它们。 有没有办法在浏览器(Firefox,Chrome等)中从Java打开网页并发送cookie?

到目前为止的代码:(是的,我知道在主方法上放置“throws Exception”在任何方面都不聪明。请忽略它,当它工作时它不会存在。)

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 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... } 

解决了:

我正在使用Firefox,所以我让程序使用SQLite访问Firefox cookie数据库并手动添加/修改cookie。 像这样的东西:

  public static void writeCookie(List nameList, List 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(); } }