Java, let CookieHandler work with only one instance

I do not know how CookieHandler works on the system, I looked at the source of CookieHandler, but did not find more information than the get / set methods. Where TCP / HTTP connections use the CookieHandler instance that I installed

CookieHandler.setDefault(...) 

Which source file should I reference? URLConnection and HttpURLConnection seem to have nothing to do with this.

Help, thanks in advance.


Edit: Is it possible to apply CookieHandler to only one instance in which setDefault is setDefault .
+4
source share
2 answers

I worked with this

 private static class DelegatingCookieManager extends CookieManager { @Override public void setCookiePolicy(CookiePolicy cookiePolicy) { delegate.get().setCookiePolicy(cookiePolicy); } @Override public CookieStore getCookieStore() { return delegate.get().getCookieStore(); } @Override public Map<String, List<String>> get( URI uri, Map<String, List<String>> requestHeaders) throws IOException { return delegate.get().get(uri, requestHeaders); } @Override public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException { delegate.get().put(uri, responseHeaders); } } 

which is installed globally

 static { CookieHandler.setDefault(new DelegatingCookieManager()); } 

but has no state and delegate

 private static final ThreadLocal<CookieManager> delegate = new ThreadLocal<CookieManager>(); 

which gets an instance in the class where it is used

 private final CookieManager ownCookieManager = new CookieManager(); 

as

 delegate.set(ownCookieManager); doRequest(); 
0
source

javadoc for java.net.CookieManager gives a pretty good overview of how CookieHandler works.

+1
source

Source: https://habr.com/ru/post/1333533/


All Articles