【发布时间】:2014-11-28 02:43:06
【问题描述】:
我试图找出特定 cookie 的设置时间,并根据其新值更新某些内容。我正在尝试通过实施我自己的 cookie 政策来做到这一点
我的问题涉及 HttpCookie.domainMatches 方法。
当我使用 CookiePolicy.ACCEPT_ORIGINAL_SERVER 策略时,它会拒绝多个 cookie。
cookie 的域为“HostName”(即 machineName123)。这显然是一个无效的域。
我应该在服务器端显式设置域吗?
如果我使用 InetAddress 获得 CanonicalHostName,我只会获得一个 IP 地址...
例如:
CookieHandler.setDefault(new CookieManager(null, new CookiePolicy() {
@Override
public boolean shouldAccept(java.net.URI uri, final java.net.HttpCookie cookie) {
if(CookiePolicy.ACCEPT_ORIGINAL_SERVER.shouldAccept(uri, cookie)) {
//custom code here
}
System.out.println("Accepting cookie uri Host: " + uri.getHost() + " name:" + cookie.getName() + " value: " + cookie.getValue() + " domain: " + cookie.getDomain() + " path: " + cookie.getPath() );
System.out.println("Domain matches uri: " + HttpCookie.domainMatches(cookie.getDomain(), uri.getHost()));
return true;
}
System.out.println("Rejecting cookie uri Host: " + uri.getHost() + " name:" + cookie.getName() + " value: " + cookie.getValue() + " domain: " + cookie.getDomain() + " path: " + cookie.getPath() );
System.out.println("Domain matches uri: " + HttpCookie.domainMatches(cookie.getDomain(), uri.getHost()));
return false;
};
}));
还有其他方法可以确定特定 cookie 的设置时间吗? (它可能发生在多个连接中),还是有其他方法解析主机名?
谢谢!
【问题讨论】:
标签: java http cookies dns hostname