Java Reference
In-Depth Information
StringTokenizer tok = new StringTokenizer(str, "=");
String name = tok.nextToken();
String value = tok.nextToken();
this.map.put(name, value);
}
n++;
} while (str != null);
}
/**
* Once you have loaded cookies with loadCookies, you can
* call saveCookies to copy these cookies to a new HTTP
* request. This allows you to easily support cookies.
*
* @param http
* The URLConnection object to add cookies to.
*/
public void saveCookies(URLConnection http)
{
StringBuilder str = new StringBuilder();
Set<String> set = this.map.keySet();
for (String key : set)
{
String value = this.map.get(key);
if (str.length() > 0)
{
str.append("; ");
}
str.append(key + "=" + value);
}
http.setRequestProperty("Cookie", str.toString());
}
}
The
CookieUtility
class is essentially a collection. It loads cookies from HTTP
server responses and then attaches them to outbound browser requests.
Reading Cookies from the Server
To support cookies from your bot, you should call the
loadCookies
meth-
od of the
CookieUtility
class. The
loadCookies
method will scan the
URLConnection
object and extract the cookies from any
Set-Cookie
headers
found. The
loadCookies
method begins by declaring a few needed variables.