Results 1 to 2 of 2
  1. #1
    Connor is offline Member
    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Default Getting cookies from a webpage

    Off this website(docs.oracle.com/...) I found the code to get cookies from a webpage:
    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;
    }
    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:
    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 08:51 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Getting cookies from a webpage

    What methods work and what don't? Add some println statements that print out the results of each step so you can see what the code does.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. CooKies and Session
    By makpandian in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 04-27-2009, 02:47 PM
  2. cookies
    By lukky in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 09-02-2008, 08:46 PM
  3. JSP cookies example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-15-2008, 04:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •