I had this post on how to login to sites using Java’s URL and HttpURLConnection classes and how there is no need to manually store a cookie’s information and pass it to every other URL you intend to visit that requires a login if it cannot find the cookie information. This neat Cookie utility by Saar Machtinger handles all storing of cookie information after you log in to a site. It only needs to read the the contents of the HttpURLConnection variable after it has successfuly posted a request to login.
Then, once you have the CookieTool variable in hand with the cookie information like session id among others, you can access any url of that certain site which you logged in to. To do so, simply do this (ct is the CookieTool variable).
|
1 2 3 4 |
URL url = new URL("http://www.mydomain.com/some_page.jsp"); HttpURLConnection httpcon = (HttpURLConnection) ct.writeCookies(url.openConnection(), false); String content = METHOD_THAT_CONVERT_INPUTSTREAM_TO_TEXT(httpcon.getInputStream()); httpcon.disconnect(); |
This is the CookieTool class by the way.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
// Changes by: Saar Machtinger, me@cawa.com import java.net.*; import java.util.*; public class CookieTool { static Hashtable theCookies = new Hashtable(); /** * Send the Hashtable (theCookies) as cookies, and write them to * the specified URLconnection * * @param urlConn The connection to write the cookies to. * @param printCookies Print or not the action taken. * * @return The urlConn with the all the cookies in it. */ public URLConnection writeCookies(URLConnection urlConn, boolean printCookies){ String cookieString = ""; Enumeration keys = theCookies.keys(); while (keys.hasMoreElements()) { String key = (String)keys.nextElement(); cookieString += key + "=" + theCookies.get(key); if (keys.hasMoreElements()) cookieString += "; "; } urlConn.setRequestProperty("Cookie", cookieString); if (printCookies) System.out.println("Wrote cookies:\n " + cookieString+"\n"); return urlConn; } /** * Read cookies from a specified URLConnection, and insert them * to the Hashtable * The hashtable represents the Cookies. * * @param urlConn the connection to read from * @param printCookies Print the cookies or not, for debugging * @param reset Clean the Hashtable or not */ public void readCookies(URLConnection urlConn, boolean printCookies, boolean reset) { if (reset) theCookies.clear(); int i=1; String hdrKey; String hdrString; String aCookie; while ((hdrKey = urlConn.getHeaderFieldKey(i)) != null) { if (hdrKey.equals("Set-Cookie")) { hdrString = urlConn.getHeaderField(i); StringTokenizer st = new StringTokenizer(hdrString,","); while (st.hasMoreTokens()) { String s = st.nextToken(); aCookie = s.substring(0, s.indexOf(";")); // aCookie = hdrString.substring(0, s.indexOf(";")); int j = aCookie.indexOf("="); if (j != -1) { if (!theCookies.containsKey(aCookie.substring(0, j))) { // if the Cookie do not already exist then when keep it, // you may want to add some logic to update // the stored Cookie instead. thanks to rwhelan theCookies.put(aCookie.substring(0, j),aCookie.substring(j + 1)); if (printCookies) { System.out.println("Reading Key: " + aCookie.substring(0, j)); System.out.println(" Val: " + aCookie.substring(j + 1)); } } } } } i++; } } /** * Display all the cookies currently in the HashTable * */ public void viewAllCookies() { System.out.println("All Cookies are:"); Enumeration keys = theCookies.keys(); String key; while (keys.hasMoreElements()) { key = (String)keys.nextElement(); System.out.println(" " + key + "=" + theCookies.get(key)); } } /** * Display the current cookies in the URLConnection, * searching for the: "Cookie" header * * This is Valid only after a writeCookies operation. * * @param urlConn The URL to print the associates cookies in. */ public void viewURLCookies(URLConnection urlConn) { System.out.print("Cookies in this URLConnection are:\n"); System.out.println(urlConn.getRequestProperty("Cookie")); } /** * Add a specific cookie, by hand, to the HastTable of the Cookies * * @param _key The Key/Name of the Cookie * @param _val The Calue of the Cookie * @param printCookies Print or not the result */ public void addCookie(String _key, String _val, boolean printCookies) { if (!theCookies.containsKey(_key)) { theCookies.put(_key, _val); if (printCookies){ System.out.println("Adding Cookie: "); System.out.println(" " + _key + " = " + _val); } } } } |
1 comment