Java,让CookieHandler只处理一个实例 [英] Java, let CookieHandler work on only one instance

查看:94
本文介绍了Java,让CookieHandler只处理一个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道CookieHandler如何工作在系统范围内,我没有查看CookieHandler的来源,但发现没有更多的信息,除了get / set方法。 TCP / HTTP连接在哪里使用CookieHandler的实例,我通过

设置

  CookieHandler.setDefault(...)

我应该参考哪个源文件? URLConnection & HttpURLConnection 似乎没有任何关系。



帮助,提前感谢。



编辑:
可以将CookieHandler仅应用于一个实例,其中 setDefault



<$>

解决方案

p $ p> private static class DelegatingCookieManager extends CookieManager {
@Override public void setCookiePolicy(CookiePolicy cookiePolicy){
delegate.get()。setCookiePolicy(cookiePolicy);
}

@Override public CookieStore getCookieStore(){
return delegate.get()。getCookieStore();
}

@覆盖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(); 

like

  delegate.set(ownCookieManager); 
doRequest();


I don't know how CookieHandler works system wide, I did view the source of CookieHandler but found no more information except the get/set methods. Where do TCP/HTTP connections use instance of CookieHandler, which I set by

CookieHandler.setDefault(...)

Which source file I should refer to? URLConnection & HttpURLConnection don't seem have things to do with it.

Help, thanks in advance.


Edit: Is it possible to apply CookieHandler to only one instance in which setDefault is invoked.

解决方案

I got it working by using 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 gets installed globally

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

but has no state and delegate to a

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

which gets instantiated in the class where it gets used

private final CookieManager ownCookieManager = new CookieManager();

like

delegate.set(ownCookieManager);
doRequest();

这篇关于Java,让CookieHandler只处理一个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆