Not sure if for the same purpose I am doing something similar in my code. When I set cookies, I want to set the domain as close to the top as possible, so I need to find the domain at a level lower than the open suffix. For example, the highest domain you can set for the cookie for host.div.example.com is .example.com . For host.div.example.co.jp there is .example.co.jp .
Unfortunately, the code is not in the public domain. It is very easy to do. I mainly use the following 2 classes from Apache HttpClient 4,
org.apache.http.impl.cookie.PublicSuffixFilter org.apache.http.impl.cookie.PublicSuffixListParser
I forgot the exact reason, but we had to do some minor tricks. You simply move the domain from top to bottom, the first valid cookie domain is what you need.
You need to download the public suffix list and include it in your JAR,
http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/src/effective_tld_names.dat?raw=1
source share