Results 1 to 2 of 2
Thread: Getting cookies from a webpage
- 01-19-2013, 07:49 PM #1
Member
- Join Date
- Jan 2013
- Posts
- 1
- Rep Power
- 0
Getting cookies from a webpage
Off this website(docs.oracle.com/...) I found the code to get cookies from a webpage:
I pasted it in eclipse to see how it works but it gave me numerous errors. So I changed it to this which didn't give me errors other than a dead code warning on the "{cookies = cookies + ";" + v;}" lines. Whenever I run it though I just get a return value of null instead of the cookies. Why? When try the same using URLConnection I have no issues. Anyways here's the current code:Java Code:String retrieveCookie(URL url) { String cookieValue = null; CookieHandler handler = CookieHandler.getDefault(); if (handler != null) { Map<String, List<String>> headers = handler.get(url.toURI(), new HashMap<String, List<String>>()); List<String> values = headers.get("Cookie"); for (Iterator<String> iter=values.iterator(); iter.hasNext();) { String v = iter.next(); if (cookieValue == null) cookieValue = v; else cookieValue = cookieValue + ";" + v; } } return cookieValue; }
Java Code:import java.io.IOException; import java.net.CookieHandler; import java.net.URISyntaxException; import java.net.URL; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; public class Main { /** * @param args */ public static void main(String[] args) throws IOException, URISyntaxException { System.out.println(getCookies(new URL("http://www.fridaystudentportal.com/passaicvalley/index.cfm"))); } private static String getCookies(URL url) throws IOException, URISyntaxException { String cookies = null; CookieHandler handler = CookieHandler.getDefault(); if (handler != null) { Map<String, List<String>> headers = handler.get(url.toURI(), new HashMap<String, List<String>>()); List<String> values = headers.get("Cookie"); Iterator<String> iter; for (iter = values.iterator(); iter.hasNext();); { String v = iter.next(); if (cookies == null) { cookies = v; } else { cookies = cookies + ";" + v; } } } return cookies; } }Last edited by Connor; 01-19-2013 at 07:51 PM.
- 01-20-2013, 02:09 AM #2
Similar Threads
-
CooKies and Session
By makpandian in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 04-27-2009, 01:47 PM -
cookies
By lukky in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 09-02-2008, 07:46 PM -
JSP cookies example
By Java Tip in forum Java TipReplies: 0Last Post: 01-15-2008, 03:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks