CookieHandler
做重活。这是我之前做的一个:
public class MyCookieHandler extends CookieHandler {
private final Map<String, List<String>> cookies =
new HashMap<String, List<String>>();
@Override public Map<String, List<String>> get(URI uri,
Map<String, List<String>> requestHeaders) throws IOException {
Map<String, List<String>> ret = new HashMap<String, List<String>>();
synchronized (cookies) {
List<String> store = cookies.get(uri.getHost());
if (store != null) {
store = Collections.unmodifiableList(store);
ret.put("Cookie", store);
}
}
return Collections.unmodifiableMap(ret);
}
@Override public void put(URI uri, Map<String, List<String>> responseHeaders)
throws IOException {
List<String> newCookies = responseHeaders.get("Set-Cookie");
if (newCookies != null) {
synchronized (cookies) {
List<String> store = cookies.get(uri.getHost());
if (store == null) {
store = new ArrayList<String>();
cookies.put(uri.getHost(), store);
}
store.addAll(newCookies);
}
}
}
}
这个
CookieHandler
假设您的cookie处理对于JVM是全局的;如果您想要每个线程的客户机会话或其他更复杂的事务处理,最好还是坚持使用手动方法。