A class that can read and write cookies via UrlConnection

zhaozj2021-02-16  54

• This class can operate the cookie of the website opened in the Java program, sometimes very useful

And through this class, you can also understand the storage method of cookie.

// Changes by: saar machine, me@cawa.comimport java.net. *; Import java.io. *; Import java.util. *;

? Class CookiesInJava {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); ???? ife (keys.hasmoreElements ()) ??????? cookiestring = ";";? ???} ?? urlconn.setRequestProperty ("cookie", cookiestring); ?? if (printcookies) ????? system.out.println ("wrote cookies: / n ??" cookiestring; ?? 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) ????? = 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, sindexof (";")); ?????????? // acookie = Hdrstring.substring (0, sindexof (";")); ????????? INT j = acookie.indexof ("="); ????????? IF (j! = -1) {???????????? IF (! Thecookies.contai Nskey (acoodie.substring (0, j))) {????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?????? // you may want to add some logic to update the stored cookie instead. ???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???? Thecookies.put (Acookie.substring (0, j), acookie.substring (j 1)); ?????????????? f (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 havehtable? *? * /? Public void viewllcookies () {?? system.out.println ("all cookies are:"); ?? enumeration keys = thecookies.keys.keys.keys (); ?? String Key; ?? While (Keys.hasMoreElements ()) {???? key = (String) keys.nextelement (); ???? system.out.println ("??" key "=" ????? "g g}); ????} ??}? / **? * 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 is: / n ??"); ?? System.out.println (UrlConnGetRequestProperty ("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); ????? f (printcookies) {???????? system.out.println ("Adding cookie:"); ???????? system.out .println ("??" _key "=" _val); ????????} ?????} ??}}

转载请注明原文地址:https://www.9cbs.com/read-21167.html

New Post(0)