我通过使用它来工作
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);
}
}
全局安装
static {
CookieHandler.setDefault(new DelegatingCookieManager());
}
但没有状态和代表
private static final ThreadLocal<CookieManager> delegate =
new ThreadLocal<CookieManager>();
在使用它的类中实例化
private final CookieManager ownCookieManager = new CookieManager();
像
delegate.set(ownCookieManager);
doRequest();