/[Apache-SVN]/httpcomponents/oac.hc3x/trunk/src/examples/BasicAuthenticationExample.java
ViewVC logotype

Contents of /httpcomponents/oac.hc3x/trunk/src/examples/BasicAuthenticationExample.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 608014 - (show annotations)
Wed Jan 2 05:48:53 2008 UTC (22 months, 3 weeks ago) by rolandw
File size: 3525 byte(s)
replaced CVS keyword Header with SVN keyword HeadURL
1 /*
2 * $HeadURL$
3 * $Revision$
4 * $Date$
5 * ====================================================================
6 *
7 * Licensed to the Apache Software Foundation (ASF) under one or more
8 * contributor license agreements. See the NOTICE file distributed with
9 * 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.getState().setCredentials(
62 new AuthScope("www.verisign.com", 443, "realm"),
63 new UsernamePasswordCredentials("username", "password")
64 );
65
66 // create a GET method that reads a file over HTTPS, we're assuming
67 // that this file requires basic authentication using the realm above.
68 GetMethod get = new GetMethod("https://www.verisign.com/products/index.html");
69
70 // Tell the GET method to automatically handle authentication. The
71 // method will use any appropriate credentials to handle basic
72 // authentication requests. Setting this value to false will cause
73 // any request for authentication to return with a status of 401.
74 // It will then be up to the client to handle the authentication.
75 get.setDoAuthentication( true );
76
77 try {
78 // execute the GET
79 int status = client.executeMethod( get );
80
81 // print the status and response
82 System.out.println(status + "\n" + get.getResponseBodyAsString());
83
84 } finally {
85 // release any connection resources used by the method
86 get.releaseConnection();
87 }
88 }
89 }

Properties

Name Value
svn:eol-style native
svn:keywords Date Author Id Revision HeadURL

apache@apache.org
ViewVC Help
Powered by ViewVC 1.1.2