在javafx webengine / webview中设置cookie

时间:2018-06-06 14:10:04

标签: javafx javafx-webengine

我无法使用com.sun.webkit.network.CookieManager在javafx中设置cookie。

    CookieManager cookieManager = (CookieManager) CookieHandler.getDefault();

    Field f = cookieManager.getClass().getDeclaredField("store");
    f.setAccessible(true);
    Object cookieStore = f.get(cookieManager);

    Field bucketsField = Class.forName("com.sun.webkit.network.CookieStore").getDeclaredField("buckets");
    bucketsField.setAccessible(true);
    Map buckets = (Map) bucketsField.get(cookieStore);
    f.setAccessible(true);


    Map<String, List<String>> cookiesByDomainMap = new HashMap<>();

    for (Object o : buckets.entrySet()) {
        Map.Entry pair = (Map.Entry) o;
        String domain = (String) pair.getKey();
        Map cookies = (Map) pair.getValue();

        List<String> cookiePairs = new ArrayList<>();   
        Collection cookieCollection = cookies.values();
        for (Object object : cookieCollection) {

            // get cookie in string format, example:
            // [name=HSID, value=AEoMUSsXuQkVcohfx, expiryTime=1591364383000, domain=google.com, path=/, creationTime=[baseTime=1528292385157, subtime=2], lastAccessTime=1528292390749, persistent=true, hostOnly=false, secureOnly=false, httpOnly=true]
            String cookie = object.toString();

            // get cookie name
            int index1 = cookie.indexOf("name=");
            int index2 = cookie.indexOf(",");
            String cookieName = cookie.substring(index1 + 5, index2);  // HSID

            // get cookie value
            cookie = cookie.substring(index2 + 2);
            index1 = cookie.indexOf("value=");
            index2 = cookie.indexOf(",");
            String cookieValue = cookie.substring(index1 + 6, index2);   // AEoMUSsXuQkVcohfx

            cookiePairs.add(cookieName + "=" + cookieValue);   // HSID=AEoMUSsXuQkVcohfx
        }

        // add to map domain and list of cookies for this domain
        cookiesByDomainMap.put(domain, cookiePairs);
    }

    CookieManager cookieManager2 = new CookieManager();

    // put cookies to CookieManager
    for(Map.Entry<String, List<String>> entry : cookiesByDomainMap.entrySet()) {
        String domain = entry.getKey();

        Map<String, List<String>> headers = new LinkedHashMap<String, List<String>>();
        headers.put("Set-Cookie", entry.getValue());
        cookieManager2.put(URI.create("http://" + domain), headers);
    }

但它不起作用。 我的新cookie管理器包含不正确的cookie: [name = HSID,value = AEoMUSsXuQkVcohfx,expiryTime = 9223372036854775807,domain = google.com,path = /,creationTime = [baseTime = 1528292391928,subtime = 5],lastAccessTime = 1528292391928,persistent = false,hostOnly = true,secureOnly = false ,httpOnly = false]

更正Cookie:[name = HSID,value = AEoMUSsXuQkVcohfx,expiryTime = 1591364383000,domain = google.com,path = /,creationTime = [baseTime = 1528292385157,subtime = 2],lastAccessTime = 1528292390749,persistent = true,hostOnly = false,secureOnly = false,httpOnly = true]

Persistent,hostOnly,httpOnly不同。

我找到了java.net.CookieManager的解决方案:

Cookie序列化:

    List<HttpCookie> httpCookies = cookieManager.getCookieStore().getCookies();
    Gson gson = new GsonBuilder().create();
    String jsonCookie = gson.toJson(httpCookies);

Cookies反序列化:

Gson gson = new GsonBuilder().create();
List<HttpCookie> httpCookies = new ArrayList<>();
Type type = new TypeToken<List<HttpCookie>>() {}.getType();
httpCookies = gson.fromJson(json, type);   // convert json string to list
for (HttpCookie cookie : httpCookies) {
    cookieManager.getCookieStore().add(URI.create(cookie.getDomain()), cookie);
}

java.net.CookieManager解决方案适用于gmail.com,但不适用于我的网站。

Gson无法将其序列化。

除了gson我试过jodd.json.JsonParser和com.fasterxml.jackson.databind.ObjectMapper

也许有人会提示好的序列化程序或其他方式来建立cookie。

0 个答案:

没有答案