1 31 32 import org.apache.commons.httpclient.HttpClient; 33 import org.apache.commons.httpclient.UsernamePasswordCredentials; 34 import org.apache.commons.httpclient.auth.AuthScope; 35 import org.apache.commons.httpclient.methods.GetMethod; 36 37 45 public class BasicAuthenticationExample { 46 47 50 public BasicAuthenticationExample() { 51 super(); 52 } 53 54 public static void main(String [] args) throws Exception { 55 HttpClient client = new HttpClient(); 56 57 client.getState().setCredentials( 62 new AuthScope("www.verisign.com", 443, "realm"), 63 new UsernamePasswordCredentials("username", "password") 64 ); 65 66 GetMethod get = new GetMethod("https://www.verisign.com/products/index.html"); 69 70 get.setDoAuthentication( true ); 76 77 try { 78 int status = client.executeMethod( get ); 80 81 System.out.println(status + "\n" + get.getResponseBodyAsString()); 83 84 } finally { 85 get.releaseConnection(); 87 } 88 } 89 } 90 | Popular Tags |