1 28 29 import org.apache.commons.httpclient.*; 30 import org.apache.commons.httpclient.cookie.CookiePolicy; 31 import org.apache.commons.httpclient.cookie.CookieSpec; 32 import org.apache.commons.httpclient.methods.*; 33 34 43 public class FormLoginDemo 44 { 45 static final String LOGON_SITE = "developer.java.sun.com"; 46 static final int LOGON_PORT = 80; 47 48 public FormLoginDemo() { 49 super(); 50 } 51 52 public static void main(String [] args) throws Exception { 53 54 HttpClient client = new HttpClient(); 55 client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http"); 56 client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); 57 61 GetMethod authget = new GetMethod("/servlet/SessionServlet"); 62 63 client.executeMethod(authget); 64 System.out.println("Login form get: " + authget.getStatusLine().toString()); 65 authget.releaseConnection(); 67 CookieSpec cookiespec = CookiePolicy.getDefaultSpec(); 69 Cookie[] initcookies = cookiespec.match( 70 LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies()); 71 System.out.println("Initial set of cookies:"); 72 if (initcookies.length == 0) { 73 System.out.println("None"); 74 } else { 75 for (int i = 0; i < initcookies.length; i++) { 76 System.out.println("- " + initcookies[i].toString()); 77 } 78 } 79 80 PostMethod authpost = new PostMethod("/servlet/SessionServlet"); 81 NameValuePair action = new NameValuePair("action", "login"); 83 NameValuePair url = new NameValuePair("url", "/index.html"); 84 NameValuePair userid = new NameValuePair("UserId", "userid"); 85 NameValuePair password = new NameValuePair("Password", "password"); 86 authpost.setRequestBody( 87 new NameValuePair[] {action, url, userid, password}); 88 89 client.executeMethod(authpost); 90 System.out.println("Login form post: " + authpost.getStatusLine().toString()); 91 authpost.releaseConnection(); 93 Cookie[] logoncookies = cookiespec.match( 97 LOGON_SITE, LOGON_PORT, "/", false, client.getState().getCookies()); 98 System.out.println("Logon cookies:"); 99 if (logoncookies.length == 0) { 100 System.out.println("None"); 101 } else { 102 for (int i = 0; i < logoncookies.length; i++) { 103 System.out.println("- " + logoncookies[i].toString()); 104 } 105 } 106 int statuscode = authpost.getStatusCode(); 109 if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) || 110 (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) || 111 (statuscode == HttpStatus.SC_SEE_OTHER) || 112 (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) { 113 Header header = authpost.getResponseHeader("location"); 114 if (header != null) { 115 String newuri = header.getValue(); 116 if ((newuri == null) || (newuri.equals(""))) { 117 newuri = "/"; 118 } 119 System.out.println("Redirect target: " + newuri); 120 GetMethod redirect = new GetMethod(newuri); 121 122 client.executeMethod(redirect); 123 System.out.println("Redirect: " + redirect.getStatusLine().toString()); 124 redirect.releaseConnection(); 126 } else { 127 System.out.println("Invalid redirect"); 128 System.exit(1); 129 } 130 } 131 } 132 } 133
| Popular Tags
|