The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.net  [27 examples] > HTTP  [4 examples]

e141. Getting the Cookies from an HTTP Connection

When the server wants to set a cookie in the client, it includes a response header of the form
    Set-Cookie: cookie-value; expires=date; path=path; domain=domain-name; secure
cookie-value is some arbitrary string data that should be returned to the server in future URL requests. The life time of the cookie is specified by expires. If expires is not specified, the cookie expires at the end of the session. When a URL request is made, the cookie should be sent along only if domain-name matches the end of the fully-qualified host name of the URL request and path matches the beginning of the path of the URL request. If secure is specified, the cookie should be sent to the server only through HTTPS.
    try {
        // Create a URLConnection object for a URL
        URL url = new URL("http://hostname:80");
        URLConnection conn = url.openConnection();
    
        // Get all cookies from the server.
        // Note: The first call to getHeaderFieldKey() will implicit send
        // the HTTP request to the server.
        for (int i=0; ; i++) {
            String headerName = conn.getHeaderFieldKey(i);
            String headerValue = conn.getHeaderField(i);
    
            if (headerName == null && headerValue == null) {
                // No more headers
                break;
            }
            if ("Set-Cookie".equalsIgnoreCase(headerName)) {
                // Parse cookie
                String[] fields = headerValue.split(";\\s*");
    
                String cookieValue = fields[0];
                String expires = null;
                String path = null;
                String domain = null;
                boolean secure = false;
    
                // Parse each field
                for (int j=1; j<fields.length; j++) {
                    if ("secure".equalsIgnoreCase(fields[j])) {
                        secure = true;
                    } else if (fields[j].indexOf('=') > 0) {
                        String[] f = fields[j].split("=");
                        if ("expires".equalsIgnoreCase(f[0])) {
                            expires = f[1];
                        } else if ("domain".equalsIgnoreCase(f[0])) {
                            domain = f[1];
                        } else if ("path".equalsIgnoreCase(f[0])) {
                            path = f[1];
                        }
                    }
                }
    
                // Save the cookie...
            }
        }
    } catch (MalformedURLException e) {
    } catch (IOException e) {
    }
Here's a sample of cookies from two websites:
    B=a43ka6gu6f4n4&b=2; expires=Thu, 15 Apr 2010 20:00:00 GMT;
        path=/; domain=.yahoo.com
    
    PREF=ID=e51:TM=686:LM=86:S=BL-w0; domain=.google.com; path=/;
        expires=Sun, 17-Jan-2038 19:14:07 GMT

 Related Examples
e140. Getting the Response Headers from an HTTP Connection
e142. Sending a Cookie to an HTTP Server
e143. Preventing Automatic Redirects in a HTTP Connection

See also: Datagram    Encodings    Hostnames and IP Addresses    Multicast    Sockets    URLs   


© 2002 Addison-Wesley.