Java,让CookieHandler只作用于一个实例

发布于 2024-10-09 16:58:42 字数 439 浏览 10 评论 0原文

我不知道 CookieHandler 在系统范围内是如何工作的,我确实查看了 CookieHandler 的源代码,但除了 get/set 方法之外没有找到更多信息。 TCP/HTTP 连接在哪里使用 CookieHandler 实例,我通过“

CookieHandler.setDefault(...)

我应该引用哪个源文件”设置该实例? URLConnection & HttpURLConnection 似乎与它无关。

帮忙,先谢谢了。


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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺ぐ爱和你が 2024-10-16 16:58:42

我通过使用 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);
    }
}

,它在全局范围内安装

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

,但没有状态并委托给 a

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

,它在使用它的类中实例化,

private final CookieManager ownCookieManager = new CookieManager();

例如

delegate.set(ownCookieManager);
doRequest();

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();
温柔少女心 2024-10-16 16:58:42

java.net.CookieManager 的 javadoc 很好地概述了 CookieHandler 如何适应。

The javadoc for java.net.CookieManager gives a pretty good overview of how CookieHandler fits in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文