1 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 51 public class CookieDemoApp { 52 53 63 public static void main(String [] args) throws Exception { 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 String strURL = args[0]; 71 System.out.println("Target URL: " + strURL); 72 73 HttpState initialState = new HttpState(); 75 Cookie mycookie = new Cookie(".foobar.com", "mycookie", "stuff", "/", null, false); 78 initialState.addCookie(mycookie); 80 81 HttpClient httpclient = new HttpClient(); 83 httpclient.getHttpConnectionManager(). 84 getParams().setConnectionTimeout(30000); 85 httpclient.setState(initialState); 86 87 httpclient.getParams().setCookiePolicy(CookiePolicy.RFC_2109); 90 93 101 GetMethod httpget = new GetMethod(strURL); 103 int result = httpclient.executeMethod(httpget); 105 System.out.println("Response status code: " + result); 107 Cookie[] cookies = httpclient.getState().getCookies(); 109 System.out.println("Present cookies: "); 111 for (int i = 0; i < cookies.length; i++) { 112 System.out.println(" - " + cookies[i].toExternalForm()); 113 } 114 httpget.releaseConnection(); 116 } 117 } 118 | Popular Tags |