我一直在尝试将Cookie从HttpsURLConnection传递到我的浏览器。不幸的是,我还没有找到......好吧,除了Android之外的任何话题都没有,这不是我想要的。 Cookie是特定于会话的,因此我每次都必须从网页下载它们。有没有办法在浏览器(Firefox,Chrome等)中从Java打开网页并发送cookie?
到目前为止的代码:(是的,我知道推出"抛出异常"主方法在任何方面都不聪明。请忽略它,当它工作时它不会出现在那里。)
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...
}
答案 0 :(得分:2)
解决:
我使用的是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();
}
}