1 31 32 import java.util.ArrayList ; 33 import java.util.List ; 34 35 import org.apache.commons.httpclient.HttpClient; 36 import org.apache.commons.httpclient.UsernamePasswordCredentials; 37 import org.apache.commons.httpclient.auth.AuthPolicy; 38 import org.apache.commons.httpclient.auth.AuthScope; 39 import org.apache.commons.httpclient.methods.GetMethod; 40 41 53 public class AlternateAuthenticationExample { 54 55 58 public AlternateAuthenticationExample() { 59 super(); 60 } 61 62 public static void main(String [] args) throws Exception { 63 HttpClient client = new HttpClient(); 64 client.getState().setCredentials( 65 new AuthScope("myhost", 80, "myrealm"), 66 new UsernamePasswordCredentials("username", "password")); 67 71 List authPrefs = new ArrayList (3); 73 authPrefs.add(AuthPolicy.BASIC); 74 authPrefs.add(AuthPolicy.NTLM); 75 authPrefs.add(AuthPolicy.DIGEST); 76 client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); 77 78 GetMethod httpget = new GetMethod("http://myhost/protected/auth-required.html"); 79 80 try { 81 int status = client.executeMethod(httpget); 82 System.out.println(httpget.getStatusLine()); 84 System.out.println(httpget.getResponseBodyAsString()); 85 } finally { 86 httpget.releaseConnection(); 88 } 89 } 90 } 91 | Popular Tags |