2
* $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 $
4
* $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
5
* ====================================================================
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
14
* http://www.apache.org/licenses/LICENSE-2.0
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
* ====================================================================
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/>.
28
* [Additional notices, if required by prior licensing conditions]
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;
38
* A simple example that uses HttpClient to perform a GET using Basic
39
* Authentication. Can be run standalone without parameters.
41
* You need to have JSSE on your classpath for JDK prior to 1.4
43
* @author Michael Becke
45
public class BasicAuthenticationExample {
48
* Constructor for BasicAuthenticatonExample.
50
public BasicAuthenticationExample() {
54
public static void main(String[] args) throws Exception {
55
HttpClient client = new HttpClient();
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")
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");
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 );
79
int status = client.executeMethod( get );
81
// print the status and response
82
System.out.println(status + "\n" + get.getResponseBodyAsString());
85
// release any connection resources used by the method
86
get.releaseConnection();