import java.io.IOException;
import java.net.CookieHandler;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
public class Fetch5 {
String urlString
= "java.sun.com";
CookieHandler.setDefault (new ListCookieHandler() );
URL url
= new URL (urlString
);
Object obj
= connection.
getContent();
url
= new URL (urlString
);
connection = url.openConnection();
obj = connection.getContent();
}
}
class ListCookieHandler extends CookieHandler {
private List<Cookie> cookieJar = new LinkedList<Cookie>();
public void put
(URI uri, Map
<String, List
<String
>> responseHeaders
) throws IOException {
List<String> setCookieList = responseHeaders.get ("Set-Cookie");
if (setCookieList != null) {
for (String item
: setCookieList
) {
Cookie cookie = new Cookie (uri, item);
for (Cookie existingCookie : cookieJar) {
if ( (cookie.getURI().equals (existingCookie.getURI() ) )
&& (cookie.getName().equals (existingCookie.getName() ) ) ) {
cookieJar.remove (existingCookie);
break;
}
}
cookieJar.add (cookie);
}
}
}
public Map
<String, List
<String
>> get
(URI uri, Map
<String, List
<String
>> requestHeaders
)
StringBuilder cookies = new StringBuilder();
for (Cookie cookie : cookieJar) {
// Remove cookies that have expired
if (cookie.hasExpired() ) {
cookieJar.remove (cookie);
} else if (cookie.matches (uri) ) {
if (cookies.length() > 0) {
cookies.append (", ");
}
cookies.append (cookie.toString() );
}
}
Map
<String, List
<String
>> cookieMap
= new HashMap
<String, List
<String
>> (requestHeaders
);
if (cookies.length() > 0) {
List
<String
> list
= Collections.
singletonList (cookies.
toString() );
cookieMap.put ("Cookie", list);
}
System.
out.
println ("CookieMap: " + cookieMap
);
}
}
class Cookie {
URI uri;
public Cookie
(URI uri,
String header
) {
String attributes
[] = header.
split (";");
String nameValue
= attributes
[0].
trim();
this.uri = uri;
this.name = nameValue.substring (0, nameValue.indexOf ('=') );
this.value = nameValue.substring (nameValue.indexOf ('=') + 1);
this.path = "/";
this.domain = uri.getHost();
for (int i = 1; i < attributes.length; i++) {
nameValue = attributes[i].trim();
int equals = nameValue.indexOf ('=');
if (equals == -1) {
continue;
}
String name
= nameValue.
substring (0, equals
);
String value
= nameValue.
substring (equals
+ 1);
if (name.equalsIgnoreCase ("domain") ) {
String uriDomain
= uri.
getHost();
if (uriDomain.equals (value) ) {
this.domain = value;
} else {
if (!value.startsWith (".") ) {
value = "." + value;
}
uriDomain = uriDomain.substring (uriDomain.indexOf ('.') );
if (!uriDomain.equals (value) ) {
}
this.domain = value;
}
} else if (name.equalsIgnoreCase ("path") ) {
this.path = value;
} else if (name.equalsIgnoreCase ("expires") ) {
try {
this.expires = expiresFormat1.parse (value);
try {
this.expires = expiresFormat2.parse (value);
}
}
}
}
}
public boolean hasExpired() {
if (expires == null) {
return false;
}
return now.after (expires);
}
return name;
}
public URI getURI() {
return uri;
}
public boolean matches (URI uri) {
if (hasExpired() ) {
return false;
}
if (path == null) {
path = "/";
}
return path.startsWith (this.path);
}
StringBuilder result = new StringBuilder (name);
result.append ("=");
result.append (value);
return result.toString();
}
}