olegk 2003/02/19 06:23:08 Modified: httpclient/src/examples CookieDemoApp.java PostXML.java Log: Cookie demo app refinement Constributed by Oleg Kalnichevski Revision Changes Path 1.8 +30 -37 jakarta-commons/httpclient/src/examples/CookieDemoApp.java Index: CookieDemoApp.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/examples/CookieDemoApp.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- CookieDemoApp.java 13 Feb 2003 09:14:43 -0000 1.7 +++ CookieDemoApp.java 19 Feb 2003 14:23:08 -0000 1.8 @@ -75,10 +75,10 @@ * HTTP GET requests. * * @author Sean C. Sullivan + * @author Oleg Kalnichevski * */ public class CookieDemoApp { - private static final String COOKIE_NAME = "count"; /** * @@ -96,42 +96,35 @@ System.err.println("<url> The url of a webpage"); System.exit(1); } - + // Get target URL String strURL = args[0]; + System.out.println("Target URL: " + strURL); - URL u = new URL(strURL); - + // Get initial state object HttpState initialState = new HttpState(); - - Cookie ck = new Cookie(".foobar.com", - COOKIE_NAME, - "0"); - - initialState.addCookie(ck); - - HttpClient client = new HttpClient(); - HostConfiguration hc = new HostConfiguration(); - hc.setHost(new URI(u)); - client.setHostConfiguration(hc); - - client.setConnectionTimeout(30000); - client.setState(initialState); - - for (int i = 0; i < 10; i++) { - GetMethod get = new GetMethod("/"); - int iResultCode = client.executeMethod(get); - HttpState state = client.getState(); - Cookie[] cookies = state.getCookies(); - for (int k = 0; k < cookies.length; k++) { - Cookie currentCookie = cookies[k]; - if (currentCookie.getName().equals(COOKIE_NAME)) { - Integer iCount = new Integer(currentCookie.getValue()); - System.out.println("count value is : " + iCount); - int iNewCount = iCount.intValue() + 1; - currentCookie.setValue("" + iNewCount); - } - } - get.releaseConnection(); + // Initial set of cookies can be retrieved from persistent storage and + // re-created, using a persistence mechanism of choice, + Cookie mycookie = new Cookie(".foobar.com", "mycookie", "stuff", "/", null, false); + // and then added to your HTTP state instance + initialState.addCookie(mycookie); + // Get HTTP client instance + HttpClient httpclient = new HttpClient(); + httpclient.setConnectionTimeout(30000); + httpclient.setState(initialState); + // Get HTTP GET method + GetMethod httpget = new GetMethod(strURL); + // Execute HTTP GET + int result = httpclient.executeMethod(httpget); + // Display status code + System.out.println("Response status code: " + result); + // Get all the cookies + Cookie[] cookies = httpclient.getState().getCookies(); + // Display the cookies + System.out.println("Present cookies: "); + for (int i = 0; i < cookies.length; i++) { + System.out.println(cookies[i].toExternalForm()); } + // Release current connection to the connection pool once you are done + httpget.releaseConnection(); } } 1.9 +6 -6 jakarta-commons/httpclient/src/examples/PostXML.java Index: PostXML.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/examples/PostXML.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- PostXML.java 17 Feb 2003 14:26:35 -0000 1.8 +++ PostXML.java 19 Feb 2003 14:23:08 -0000 1.9 @@ -126,13 +126,13 @@ // Get HTTP client HttpClient httpclient = new HttpClient(); // Execute request - int iResultCode = httpclient.executeMethod(post); + int result = httpclient.executeMethod(post); // Display status code - System.out.println("Response status code: " + iResultCode); + System.out.println("Response status code: " + result); // Display response System.out.println("Response body: "); System.out.println(post.getResponseBodyAsString()); - // Release current connection to the connection pool + // Release current connection to the connection pool once you are done post.releaseConnection(); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]