KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > CookieDemoApp


1 /*
2  * $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 $
3  * $Revision: 480424 $
4  * $Date: 2006-11-29 05:56:49 +0000 (Wed, 29 Nov 2006) $
5  * ====================================================================
6  *
7  * Licensed to the Apache Software Foundation (ASF) under one or more
8  * contributor license agreements. See the NOTICE file distributed with
9  * this work for additional information regarding copyright ownership.
10  * The ASF licenses this file to You under the Apache License, Version 2.0
11  * (the "License"); you may not use this file except in compliance with
12  * the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ====================================================================
22  *
23  * This software consists of voluntary contributions made by many
24  * individuals on behalf of the Apache Software Foundation. For more
25  * information on the Apache Software Foundation, please see
26  * <http://www.apache.org/>.
27  *
28  * [Additional notices, if required by prior licensing conditions]
29  *
30  */

31
32 import org.apache.commons.httpclient.Cookie;
33 import org.apache.commons.httpclient.HttpClient;
34 import org.apache.commons.httpclient.HttpState;
35 import org.apache.commons.httpclient.cookie.CookiePolicy;
36 import org.apache.commons.httpclient.methods.GetMethod;
37
38 /**
39  *
40  * This is a sample application that demonstrates
41  * how to use the Jakarta HttpClient API.
42  *
43  * This application sets an HTTP cookie and
44  * updates the cookie's value across multiple
45  * HTTP GET requests.
46  *
47  * @author Sean C. Sullivan
48  * @author Oleg Kalnichevski
49  *
50  */

51 public class CookieDemoApp {
52
53     /**
54      *
55      * Usage:
56      * java CookieDemoApp http://mywebserver:80/
57      *
58      * @param args command line arguments
59      * Argument 0 is a URL to a web server
60      *
61      *
62      */

63     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
64         if (args.length != 1) {
65             System.err.println("Usage: java CookieDemoApp <url>");
66             System.err.println("<url> The url of a webpage");
67             System.exit(1);
68         }
69         // Get target URL
70
String JavaDoc strURL = args[0];
71         System.out.println("Target URL: " + strURL);
72
73         // Get initial state object
74
HttpState initialState = new HttpState();
75         // Initial set of cookies can be retrieved from persistent storage and
76
// re-created, using a persistence mechanism of choice,
77
Cookie mycookie = new Cookie(".foobar.com", "mycookie", "stuff", "/", null, false);
78         // and then added to your HTTP state instance
79
initialState.addCookie(mycookie);
80
81         // Get HTTP client instance
82
HttpClient httpclient = new HttpClient();
83         httpclient.getHttpConnectionManager().
84             getParams().setConnectionTimeout(30000);
85         httpclient.setState(initialState);
86
87         // RFC 2101 cookie management spec is used per default
88
// to parse, validate, format & match cookies
89
httpclient.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
90         // A different cookie management spec can be selected
91
// when desired
92

93         //httpclient.getParams().setCookiePolicy(CookiePolicy.NETSCAPE);
94
// Netscape Cookie Draft spec is provided for completeness
95
// You would hardly want to use this spec in real life situations
96
//httppclient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
97
// Compatibility policy is provided in order to mimic cookie
98
// management of popular web browsers that is in some areas
99
// not 100% standards compliant
100

101         // Get HTTP GET method
102
GetMethod httpget = new GetMethod(strURL);
103         // Execute HTTP GET
104
int result = httpclient.executeMethod(httpget);
105         // Display status code
106
System.out.println("Response status code: " + result);
107         // Get all the cookies
108
Cookie[] cookies = httpclient.getState().getCookies();
109         // Display the cookies
110
System.out.println("Present cookies: ");
111         for (int i = 0; i < cookies.length; i++) {
112             System.out.println(" - " + cookies[i].toExternalForm());
113         }
114         // Release current connection to the connection pool once you are done
115
httpget.releaseConnection();
116     }
117 }
118
Popular Tags