001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/examples/CookieDemoApp.java,v 1.14 2004/02/22 18:08:45 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: * ====================================================================
006: *
007: * Licensed to the Apache Software Foundation (ASF) under one or more
008: * contributor license agreements. See the NOTICE file distributed with
009: * this work for additional information regarding copyright ownership.
010: * The ASF licenses this file to You under the Apache License, Version 2.0
011: * (the "License"); you may not use this file except in compliance with
012: * the License. You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: * ====================================================================
022: *
023: * This software consists of voluntary contributions made by many
024: * individuals on behalf of the Apache Software Foundation. For more
025: * information on the Apache Software Foundation, please see
026: * <http://www.apache.org/>.
027: *
028: * [Additional notices, if required by prior licensing conditions]
029: *
030: */
031:
032: import org.apache.commons.httpclient.Cookie;
033: import org.apache.commons.httpclient.HttpClient;
034: import org.apache.commons.httpclient.HttpState;
035: import org.apache.commons.httpclient.cookie.CookiePolicy;
036: import org.apache.commons.httpclient.methods.GetMethod;
037:
038: /**
039: *
040: * This is a sample application that demonstrates
041: * how to use the Jakarta HttpClient API.
042: *
043: * This application sets an HTTP cookie and
044: * updates the cookie's value across multiple
045: * HTTP GET requests.
046: *
047: * @author Sean C. Sullivan
048: * @author Oleg Kalnichevski
049: *
050: */
051: public class CookieDemoApp {
052:
053: /**
054: *
055: * Usage:
056: * java CookieDemoApp http://mywebserver:80/
057: *
058: * @param args command line arguments
059: * Argument 0 is a URL to a web server
060: *
061: *
062: */
063: public static void main(String[] args) throws Exception {
064: if (args.length != 1) {
065: System.err.println("Usage: java CookieDemoApp <url>");
066: System.err.println("<url> The url of a webpage");
067: System.exit(1);
068: }
069: // Get target URL
070: String strURL = args[0];
071: System.out.println("Target URL: " + strURL);
072:
073: // Get initial state object
074: HttpState initialState = new HttpState();
075: // Initial set of cookies can be retrieved from persistent storage and
076: // re-created, using a persistence mechanism of choice,
077: Cookie mycookie = new Cookie(".foobar.com", "mycookie",
078: "stuff", "/", null, false);
079: // and then added to your HTTP state instance
080: initialState.addCookie(mycookie);
081:
082: // Get HTTP client instance
083: HttpClient httpclient = new HttpClient();
084: httpclient.getHttpConnectionManager().getParams()
085: .setConnectionTimeout(30000);
086: httpclient.setState(initialState);
087:
088: // RFC 2101 cookie management spec is used per default
089: // to parse, validate, format & match cookies
090: httpclient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
091: // A different cookie management spec can be selected
092: // when desired
093:
094: //httpclient.getParams().setCookiePolicy(CookiePolicy.NETSCAPE);
095: // Netscape Cookie Draft spec is provided for completeness
096: // You would hardly want to use this spec in real life situations
097: //httppclient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
098: // Compatibility policy is provided in order to mimic cookie
099: // management of popular web browsers that is in some areas
100: // not 100% standards compliant
101:
102: // Get HTTP GET method
103: GetMethod httpget = new GetMethod(strURL);
104: // Execute HTTP GET
105: int result = httpclient.executeMethod(httpget);
106: // Display status code
107: System.out.println("Response status code: " + result);
108: // Get all the cookies
109: Cookie[] cookies = httpclient.getState().getCookies();
110: // Display the cookies
111: System.out.println("Present cookies: ");
112: for (int i = 0; i < cookies.length; i++) {
113: System.out.println(" - " + cookies[i].toExternalForm());
114: }
115: // Release current connection to the connection pool once you are done
116: httpget.releaseConnection();
117: }
118: }
|