Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thanks everyone.</p> <p>I upvoted all the answers, yet none had a complete solution.</p> <p>Since google'ing this problem leads to this page here, I'll post the complete solution and accept my own answer:</p> <p>HowTo:</p> <h1>1 Extend <code>CookieHandler</code> to <code>SessionCookieManager</code></h1> <p>this is based on <a href="https://stackoverflow.com/questions/10967883/how-to-use-different-cookies-for-each-connection-using-httpurlconnection-and-the">How to use different cookies for each connection using HttpURLConnection and the CookieManager in Java</a> , nivs describes it correctly, doesn't provide a full solution tho. So most/all credit goes to him, I'm just making the complete HowTo. The SessionCookieManager is based on <code>Java</code> 's source code <a href="http://docs.oracle.com/javase/7/docs/api/java/net/CookieManager.html" rel="nofollow noreferrer">http://docs.oracle.com/javase/7/docs/api/java/net/CookieManager.html</a></p> <pre><code>import java.io.IOException; import java.net.CookieHandler; import java.net.CookiePolicy; import java.net.CookieStore; import java.net.HttpCookie; import java.net.URI; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Map; public class SessionCookieManager extends CookieHandler { private CookiePolicy policyCallback; public SessionCookieManager() { this(null, null); } private final static SessionCookieManager ms_instance = new SessionCookieManager(); public static SessionCookieManager getInstance() { return ms_instance; } private final static ThreadLocal&lt;CookieStore&gt; ms_cookieJars = new ThreadLocal&lt;CookieStore&gt;() { @Override protected synchronized CookieStore initialValue() { return new InMemoryCookieStore(); } }; public void clear() { getCookieStore().removeAll(); } public SessionCookieManager(CookieStore store, CookiePolicy cookiePolicy) { // use default cookie policy if not specify one policyCallback = (cookiePolicy == null) ? CookiePolicy.ACCEPT_ALL //note that I changed it to ACCEPT_ALL : cookiePolicy; // if not specify CookieStore to use, use default one } public void setCookiePolicy(CookiePolicy cookiePolicy) { if (cookiePolicy != null) policyCallback = cookiePolicy; } public CookieStore getCookieStore() { return ms_cookieJars.get(); } public Map&lt;String, List&lt;String&gt;&gt; get(URI uri, Map&lt;String, List&lt;String&gt;&gt; requestHeaders) throws IOException { // pre-condition check if (uri == null || requestHeaders == null) { throw new IllegalArgumentException("Argument is null"); } Map&lt;String, List&lt;String&gt;&gt; cookieMap = new java.util.HashMap&lt;String, List&lt;String&gt;&gt;(); // if there's no default CookieStore, no way for us to get any cookie if (getCookieStore() == null) return Collections.unmodifiableMap(cookieMap); List&lt;HttpCookie&gt; cookies = new java.util.ArrayList&lt;HttpCookie&gt;(); for (HttpCookie cookie : getCookieStore().get(uri)) { // apply path-matches rule (RFC 2965 sec. 3.3.4) if (pathMatches(uri.getPath(), cookie.getPath())) { cookies.add(cookie); } } // apply sort rule (RFC 2965 sec. 3.3.4) List&lt;String&gt; cookieHeader = sortByPath(cookies); cookieMap.put("Cookie", cookieHeader); return Collections.unmodifiableMap(cookieMap); } public void put(URI uri, Map&lt;String, List&lt;String&gt;&gt; responseHeaders) throws IOException { // pre-condition check if (uri == null || responseHeaders == null) { throw new IllegalArgumentException("Argument is null"); } // if there's no default CookieStore, no need to remember any cookie if (getCookieStore() == null) return; for (String headerKey : responseHeaders.keySet()) { // RFC 2965 3.2.2, key must be 'Set-Cookie2' // we also accept 'Set-Cookie' here for backward compatibility if (headerKey == null || !(headerKey.equalsIgnoreCase("Set-Cookie2") || headerKey.equalsIgnoreCase("Set-Cookie") ) ) { continue; } for (String headerValue : responseHeaders.get(headerKey)) { try { List&lt;HttpCookie&gt; cookies = HttpCookie.parse(headerValue); for (HttpCookie cookie : cookies) { if (shouldAcceptInternal(uri, cookie)) { getCookieStore().add(uri, cookie); } } } catch (IllegalArgumentException e) { // invalid set-cookie header string // no-op } } } } /* ---------------- Private operations -------------- */ // to determine whether or not accept this cookie private boolean shouldAcceptInternal(URI uri, HttpCookie cookie) { try { return policyCallback.shouldAccept(uri, cookie); } catch (Exception ignored) { // pretect against malicious callback return false; } } /* * path-matches algorithm, as defined by RFC 2965 */ private boolean pathMatches(String path, String pathToMatchWith) { if (path == pathToMatchWith) return true; if (path == null || pathToMatchWith == null) return false; if (path.startsWith(pathToMatchWith)) return true; return false; } /* * sort cookies with respect to their path: those with more specific Path attributes * precede those with less specific, as defined in RFC 2965 sec. 3.3.4 */ private List&lt;String&gt; sortByPath(List&lt;HttpCookie&gt; cookies) { Collections.sort(cookies, new CookiePathComparator()); List&lt;String&gt; cookieHeader = new java.util.ArrayList&lt;String&gt;(); for (HttpCookie cookie : cookies) { // Netscape cookie spec and RFC 2965 have different format of Cookie // header; RFC 2965 requires a leading $Version="1" string while Netscape // does not. // The workaround here is to add a $Version="1" string in advance if (cookies.indexOf(cookie) == 0 &amp;&amp; cookie.getVersion() &gt; 0) { cookieHeader.add("$Version=\"1\""); } cookieHeader.add(cookie.toString()); } return cookieHeader; } static class CookiePathComparator implements Comparator&lt;HttpCookie&gt; { public int compare(HttpCookie c1, HttpCookie c2) { if (c1 == c2) return 0; if (c1 == null) return -1; if (c2 == null) return 1; // path rule only applies to the cookies with same name if (!c1.getName().equals(c2.getName())) return 0; // those with more specific Path attributes precede those with less specific if (c1.getPath().startsWith(c2.getPath())) return -1; else if (c2.getPath().startsWith(c1.getPath())) return 1; else return 0; } } } </code></pre> <p>Note that in my case I changed the default value of <code>CookiePolicy</code> to <code>ACCEPT_ALL</code></p> <h1>2 In global scope, before running any threads, call:</h1> <pre><code>CookieHandler.setDefault(SessionCookieManager.getInstance()); </code></pre> <h1>3 When your thread is finished, call inside of it:</h1> <pre><code>SessionCookieManager.getInstance().clear(); </code></pre> <p>again: not my idea, just putting it together. All credit goes to <code>Java</code> and <a href="https://stackoverflow.com/users/1442259/nivs">https://stackoverflow.com/users/1442259/nivs</a> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload