~testplan-team/testplan/source-collection

« back to all changes in this revision

Viewing changes to commons-httpclient-3.1/src/examples/BasicAuthenticationExample.java

  • Committer: edA-qa mort-ora-y
  • Date: 2009-01-28 15:36:51 UTC
  • Revision ID: eda-qa@disemia.com-20090128153651-u4uzd035zyjea16j
adding httpclient

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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 $
 
3
 * $Revision: 480424 $
 
4
 * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
 
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
}