KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > TrivialApp


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/examples/TrivialApp.java,v 1.13.2.1 2004/02/22 18:21:12 olegk Exp $
3  * $Revision: 1.13.2.1 $
4  * $Date: 2004/02/22 18:21:12 $
5  *
6  * ====================================================================
7  *
8  * Copyright 2002-2004 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * 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 java.io.IOException JavaDoc;
33
34 import org.apache.commons.httpclient.Credentials;
35 import org.apache.commons.httpclient.Header;
36 import org.apache.commons.httpclient.HttpClient;
37 import org.apache.commons.httpclient.HttpException;
38 import org.apache.commons.httpclient.HttpMethod;
39 import org.apache.commons.httpclient.UsernamePasswordCredentials;
40 import org.apache.commons.httpclient.methods.GetMethod;
41
42 /**
43  *
44  * This is a simple text mode application that demonstrates
45  * how to use the Jakarta HttpClient API.
46  *
47  * @author <a HREF="mailto:jsdever@apache.org">Jeff Dever</a>
48  * @author Ortwin Glück
49  */

50 public class TrivialApp
51 {
52
53     private static final void printUsage()
54     {
55         System.out.println();
56         System.out.println("Usage: java -classpath <classpath> [-Dorg.apache.commons.logging.simplelog.defaultlog=<loglevel>] TrivialApp <url> [<username> <password>]");
57         System.out.println("<classpath> - must contain the commons-httpclient.jar and commons-logging.jar");
58         System.out.println("<loglevel> - one of error, warn, info, debug, trace");
59         System.out.println("<url> - some valid URL");
60         System.out.println("<username> - username for protected page");
61         System.out.println("<password> - password for protected page");
62         System.out.println();
63     }
64
65     public static void main(String JavaDoc[] args)
66     {
67         if ((args.length != 1) && (args.length != 3)) {
68             printUsage();
69             System.exit(-1);
70         }
71
72         Credentials creds = null;
73         if (args.length >= 3) {
74             creds = new UsernamePasswordCredentials(args[1], args[2]);
75         }
76
77         //create a singular HttpClient object
78
HttpClient client = new HttpClient();
79
80         //establish a connection within 5 seconds
81
client.setConnectionTimeout(5000);
82
83         //set the default credentials
84
if (creds != null) {
85             client.getState().setCredentials(null, null, creds);
86         }
87
88         String JavaDoc url = args[0];
89         HttpMethod method = null;
90
91         //create a method object
92
method = new GetMethod(url);
93             method.setFollowRedirects(true);
94             method.setStrictMode(false);
95         //} catch (MalformedURLException murle) {
96
// System.out.println("<url> argument '" + url
97
// + "' is not a valid URL");
98
// System.exit(-2);
99
//}
100

101         //execute the method
102
String JavaDoc responseBody = null;
103         try{
104             client.executeMethod(method);
105             responseBody = method.getResponseBodyAsString();
106         } catch (HttpException he) {
107             System.err.println("Http error connecting to '" + url + "'");
108             System.err.println(he.getMessage());
109             System.exit(-4);
110         } catch (IOException JavaDoc ioe){
111             System.err.println("Unable to connect to '" + url + "'");
112             System.exit(-3);
113         }
114
115
116         //write out the request headers
117
System.out.println("*** Request ***");
118         System.out.println("Request Path: " + method.getPath());
119         System.out.println("Request Query: " + method.getQueryString());
120         Header[] requestHeaders = method.getRequestHeaders();
121         for (int i=0; i<requestHeaders.length; i++){
122             System.out.print(requestHeaders[i]);
123         }
124
125         //write out the response headers
126
System.out.println("*** Response ***");
127         System.out.println("Status Line: " + method.getStatusLine());
128         Header[] responseHeaders = method.getResponseHeaders();
129         for (int i=0; i<responseHeaders.length; i++){
130             System.out.print(responseHeaders[i]);
131         }
132
133         //write out the response body
134
System.out.println("*** Response Body ***");
135         System.out.println(responseBody);
136
137         //clean up the connection resources
138
method.releaseConnection();
139         method.recycle();
140
141         System.exit(0);
142     }
143 }
144
Popular Tags