将Cookie从Java传递到浏览器 [英] Passing Cookies from Java to Browser

查看:430
本文介绍了将Cookie从Java传递到浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

代码到目前为止:(是的,我知道throws Exception对主方法不是聪明的,请忽略它,它不会存在,当这个工作。)

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


推荐答案

解决方案:

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

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

这篇关于将Cookie从Java传递到浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆