public class MyCookieHandler extends Object
MyCookieHandler cm = new MyCookieHandler(); URL url = new URL("http://www.hccp.org/test/cookieTest.jsp"); . . . // getting cookies: URLConnection conn = url.openConnection(); conn.connect(); // setting cookies cm.storeCookies(conn); cm.setCookies(url.openConnection());MyCookieHandler abstracts all the following java api code...
This is a brief overview on how to retrieve cookies from HTTP responses and how to return cookies in HTTP requests to the appropriate server using the java.net.* APIs.
URL myUrl = new URL("http://www.hccp.org/cookieTest.jsp"); URLConnection urlConn = myUrl.openConnection(); urlConn.connect();
Since a server may set multiple cookies in a single request, we will need to loop through the response headers, looking for all headers named "Set-Cookie".
String headerName=null; for (int i=1; (headerName = uc.getHeaderFieldKey(i))!=null; i++) { if (headerName.equals("Set-Cookie")) { String cookie = urlConn.getHeaderField(i); ...
The string returned by the getHeaderField(int index) method is a series of name=value separated by semi-colons (;). The first name/value pairing is actual data string we are interested in (i.e. "sessionId=0949eeee22222rtg" or "userId=igbrown"), the subsequent name/value pairings are meta-information that we would use to manage the storage of the cookie (when it expires, etc.).
cookie = cookie.substring(0, cookie.indexOf(";")); String cookieName = cookie.substring(0, cookie.indexOf("=")); String cookieValue = cookie.substring(cookie.indexOf("=") + 1, cookie.length());This is basically it. We now have the cookie name (cookieName) and the cookie value (cookieValue).
URL myUrl = new URL("http://www.hccp.org/cookieTest.jsp"); URLConnection urlConn = myUrl.openConnection();
String myCookie = "userId=igbrown";
Using the setRequestProperty(String name, String value); method, we will add a property named "Cookie", passing the cookie string created in the previous step as the property value.
urlConn.setRequestProperty("Cookie", myCookie);
To send the cookie, simply call connect() on the URLConnection for which we have added the cookie property:
urlConn.connect()
String myCookies = "userId=igbrown; sessionId=SID77689211949; isAuthenticated=true";This string contains three cookies (userId, sessionId, and isAuthenticated). Separate cookie name/value pairs with "; " (semicolon and whitespace).
|
|
code goes here... |
Modifier and Type | Field and Description |
---|---|
private static String |
COOKIE |
private static String |
COOKIE_VALUE_DELIMITER |
private static String |
DATE_FORMAT |
private DateFormat |
dateFormat |
private static char |
DOT |
private static String |
EXPIRES |
private static char |
NAME_VALUE_SEPARATOR |
private static String |
PATH |
private static String |
SET_COOKIE |
private static String |
SET_COOKIE_SEPARATOR |
private Map<String,Map> |
store |
Constructor and Description |
---|
MyCookieHandler()
Defaulkt Constructor.
|
Modifier and Type | Method and Description |
---|---|
private boolean |
comparePaths(String cookiePath,
String targetPath) |
private String |
getDomainFromHost(String host) |
private boolean |
isNotExpired(String cookieExpires) |
static void |
main(String[] args) |
void |
setCookies(URLConnection conn)
Prior to opening a URLConnection, calling this method will set all
unexpired cookies that match the path or subpaths for thi underlying URL
The connection MUST NOT have been opened
method or an IOException will be thrown.
|
void |
storeCookies(URLConnection conn)
Retrieves and stores cookies returned by the host on the other side
of the the open java.net.URLConnection.
|
String |
toString()
Returns a string representation of stored cookies organized by domain.
|
private static final String SET_COOKIE
private static final String COOKIE_VALUE_DELIMITER
private static final String PATH
private static final String EXPIRES
private static final String DATE_FORMAT
private static final String SET_COOKIE_SEPARATOR
private static final String COOKIE
private static final char NAME_VALUE_SEPARATOR
private static final char DOT
private DateFormat dateFormat
public MyCookieHandler()
public void storeCookies(URLConnection conn) throws IOException
conn
- a java.net.URLConnection - must be open, or IOException will be thrownIOException
- Thrown if conn is not open.public void setCookies(URLConnection conn) throws IOException
conn
- a java.net.URLConnection - must NOT be open, or IOException will be thrownIOException
- Thrown if conn has already been opened.private String getDomainFromHost(String host)
private boolean isNotExpired(String cookieExpires)
private boolean comparePaths(String cookiePath, String targetPath)
public String toString()
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses