I am trying to make multiple connections via threads.
But every connection seems to override the other's cookies, resulting in the connections using the wrong cookies.
inside the threaded class's constructor:
manager = new CookieManager();
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(manager);
Any way to manage the cookies per thread or per class?
New failed try:
Now every thread is using it's own index, yet they still seem to override each other cookie-wise. Any ideas?
public class threadedCookieStore implements CookieStore, Runnable {
CookieStore[] store = new CookieStore[1000];
int index;
public threadedCookieStore(int new_index) {
index = new_index;
// get the default in memory cookie store
store[index] = new CookieManager().getCookieStore();
// todo: read in cookies from persistant storage
// and add them store
// add a shutdown hook to write out the in memory cookies
Runtime.getRuntime().addShutdownHook(new Thread(this));
}
public void run() {
// todo: write cookies in store to persistent storage
}
public void add(URI uri, HttpCookie cookie) {
store[index].add(uri, cookie);
}
public List<HttpCookie> get(URI uri) {
return store[index].get(uri);
}
public List<HttpCookie> getCookies() {
return store[index].getCookies();
}
public List<URI> getURIs() {
return store[index].getURIs();
}
public boolean remove(URI uri, HttpCookie cookie) {
return store[index].remove(uri, cookie);
}
public boolean removeAll() {
return store[index].removeAll();
}
}
Within the class:
threadedCookieStore cookiestore = new threadedCookieStore(index);
manager = new CookieManager(cookiestore,CookiePolicy.ACCEPT_ALL);
manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(manager);
Thanks everyone.
I upvoted all the answers, yet none had a complete solution.
Since google'ing this problem leads to this page here, I'll post the complete solution and accept my own answer:
HowTo:
1 Extend
CookieHandler
toSessionCookieManager
this is based on How to use different cookies for each connection using HttpURLConnection and the CookieManager in Java , 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
Java
's source code http://docs.oracle.com/javase/7/docs/api/java/net/CookieManager.htmlNote that in my case I changed the default value of
CookiePolicy
toACCEPT_ALL
2 In global scope, before running any threads, call:
3 When your thread is finished, call inside of it:
again: not my idea, just putting it together. All credit goes to
Java
and https://stackoverflow.com/users/1442259/nivsYou could install a CookieHandler which manages ThreadLocal CookieManager instances.
The
ThreadLocal
CookieStore
by DavidBlackledge is imo the best way to go. For the sake of memory efficiency I'm providing here a simple implementation of a regularCookieStore
so you don't have to instantiate a wholeCookieManager
for each thread (assuming you have more than just a few).So if you have this CookieJar then you can change the
ms_cookieJars
declaration to this:Thanks, I tried to use your answer, but it was based on an old version of CookieManager (probably why you had to use ACCEPT_ALL) and referenced the package-private InMemoryCookieStore so it inspired me to the final solution. Should have been obvious to all of us before: a ThreadLocal CookieStore proxy class.
with
Seems to be working like a charm for me
How about a ThreadLocal CookieManager? Same idea as some of the other answers but seems to require less code:
You can set a different path for the cookies. Thus it will not be overwritten.
http://docs.oracle.com/javase/6/docs/api/java/net/HttpCookie.html#setPath%28java.lang.String%29