01: /*
02: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/examples/BasicAuthenticationExample.java,v 1.4 2004/06/12 22:47:23 olegk Exp $
03: * $Revision: 480424 $
04: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
05: * ====================================================================
06: *
07: * Licensed to the Apache Software Foundation (ASF) under one or more
08: * contributor license agreements. See the NOTICE file distributed with
09: * this work for additional information regarding copyright ownership.
10: * The ASF licenses this file to You under the Apache License, Version 2.0
11: * (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software
17: * distributed under the License is distributed on an "AS IS" BASIS,
18: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19: * See the License for the specific language governing permissions and
20: * limitations under the License.
21: * ====================================================================
22: *
23: * This software consists of voluntary contributions made by many
24: * individuals on behalf of the Apache Software Foundation. For more
25: * information on the Apache Software Foundation, please see
26: * <http://www.apache.org/>.
27: *
28: * [Additional notices, if required by prior licensing conditions]
29: *
30: */
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: /**
38: * A simple example that uses HttpClient to perform a GET using Basic
39: * Authentication. Can be run standalone without parameters.
40: *
41: * You need to have JSSE on your classpath for JDK prior to 1.4
42: *
43: * @author Michael Becke
44: */
45: public class BasicAuthenticationExample {
46:
47: /**
48: * Constructor for BasicAuthenticatonExample.
49: */
50: public BasicAuthenticationExample() {
51: super ();
52: }
53:
54: public static void main(String[] args) throws Exception {
55: HttpClient client = new HttpClient();
56:
57: // pass our credentials to HttpClient, they will only be used for
58: // authenticating to servers with realm "realm" on the host
59: // "www.verisign.com", to authenticate against
60: // an arbitrary realm or host change the appropriate argument to null.
61: client
62: .getState()
63: .setCredentials(
64: new AuthScope("www.verisign.com", 443, "realm"),
65: new UsernamePasswordCredentials("username",
66: "password"));
67:
68: // create a GET method that reads a file over HTTPS, we're assuming
69: // that this file requires basic authentication using the realm above.
70: GetMethod get = new GetMethod(
71: "https://www.verisign.com/products/index.html");
72:
73: // Tell the GET method to automatically handle authentication. The
74: // method will use any appropriate credentials to handle basic
75: // authentication requests. Setting this value to false will cause
76: // any request for authentication to return with a status of 401.
77: // It will then be up to the client to handle the authentication.
78: get.setDoAuthentication(true);
79:
80: try {
81: // execute the GET
82: int status = client.executeMethod(get);
83:
84: // print the status and response
85: System.out.println(status + "\n"
86: + get.getResponseBodyAsString());
87:
88: } finally {
89: // release any connection resources used by the method
90: get.releaseConnection();
91: }
92: }
93: }
|