将 Cookie 从 Java 传递到浏览器

Posted

技术标签:

【中文标题】将 Cookie 从 Java 传递到浏览器【英文标题】:Passing Cookies from Java to Browser 【发布时间】:2014-07-24 15:55:47 【问题描述】:

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

到目前为止的代码:(是的,我知道在 main 方法上放置“抛出异常”无论如何都不聪明。请忽略它,当它工作时它不会在那里。)

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

【问题讨论】:

【参考方案1】:

已解决:

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

  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();
    
  

【讨论】:

以上是关于将 Cookie 从 Java 传递到浏览器的主要内容,如果未能解决你的问题,请参考以下文章

跨域后cookie怎么不能传递到客户端的浏览器

cookie

java代码如何读取到浏览器中的cookie值

Spring Cloud 之 Cookie 丢失 与 Host 传递

java 怎么把token放进cookie

如何通过 CORS 传递 cookie?