001: /*
002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/examples/TrivialApp.java,v 1.18 2004/06/12 22:47:23 olegk Exp $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: * [Additional notices, if required by prior licensing conditions]
030: *
031: */
032:
033: import java.io.IOException;
034:
035: import org.apache.commons.httpclient.Credentials;
036: import org.apache.commons.httpclient.Header;
037: import org.apache.commons.httpclient.HttpClient;
038: import org.apache.commons.httpclient.HttpException;
039: import org.apache.commons.httpclient.HttpMethod;
040: import org.apache.commons.httpclient.UsernamePasswordCredentials;
041: import org.apache.commons.httpclient.auth.AuthScope;
042: import org.apache.commons.httpclient.methods.GetMethod;
043:
044: /**
045: *
046: * This is a simple text mode application that demonstrates
047: * how to use the Jakarta HttpClient API.
048: *
049: * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
050: * @author Ortwin Glück
051: */
052: public class TrivialApp {
053:
054: private static final void printUsage() {
055: System.out.println();
056: System.out
057: .println("Usage: java -classpath <classpath> [-Dorg.apache.commons.logging.simplelog.defaultlog=<loglevel>] TrivialApp <url> [<username> <password>]");
058: System.out
059: .println("<classpath> - must contain the commons-httpclient.jar and commons-logging.jar");
060: System.out
061: .println("<loglevel> - one of error, warn, info, debug, trace");
062: System.out.println("<url> - some valid URL");
063: System.out.println("<username> - username for protected page");
064: System.out.println("<password> - password for protected page");
065: System.out.println();
066: }
067:
068: public static void main(String[] args) {
069: if ((args.length != 1) && (args.length != 3)) {
070: printUsage();
071: System.exit(-1);
072: }
073:
074: Credentials creds = null;
075: if (args.length >= 3) {
076: creds = new UsernamePasswordCredentials(args[1], args[2]);
077: }
078:
079: //create a singular HttpClient object
080: HttpClient client = new HttpClient();
081:
082: //establish a connection within 5 seconds
083: client.getHttpConnectionManager().getParams()
084: .setConnectionTimeout(5000);
085:
086: //set the default credentials
087: if (creds != null) {
088: client.getState().setCredentials(AuthScope.ANY, creds);
089: }
090:
091: String url = args[0];
092: HttpMethod method = null;
093:
094: //create a method object
095: method = new GetMethod(url);
096: method.setFollowRedirects(true);
097: //} catch (MalformedURLException murle) {
098: // System.out.println("<url> argument '" + url
099: // + "' is not a valid URL");
100: // System.exit(-2);
101: //}
102:
103: //execute the method
104: String responseBody = null;
105: try {
106: client.executeMethod(method);
107: responseBody = method.getResponseBodyAsString();
108: } catch (HttpException he) {
109: System.err
110: .println("Http error connecting to '" + url + "'");
111: System.err.println(he.getMessage());
112: System.exit(-4);
113: } catch (IOException ioe) {
114: System.err.println("Unable to connect to '" + url + "'");
115: System.exit(-3);
116: }
117:
118: //write out the request headers
119: System.out.println("*** Request ***");
120: System.out.println("Request Path: " + method.getPath());
121: System.out.println("Request Query: " + method.getQueryString());
122: Header[] requestHeaders = method.getRequestHeaders();
123: for (int i = 0; i < requestHeaders.length; i++) {
124: System.out.print(requestHeaders[i]);
125: }
126:
127: //write out the response headers
128: System.out.println("*** Response ***");
129: System.out.println("Status Line: " + method.getStatusLine());
130: Header[] responseHeaders = method.getResponseHeaders();
131: for (int i = 0; i < responseHeaders.length; i++) {
132: System.out.print(responseHeaders[i]);
133: }
134:
135: //write out the response body
136: System.out.println("*** Response Body ***");
137: System.out.println(responseBody);
138:
139: //clean up the connection resources
140: method.releaseConnection();
141:
142: System.exit(0);
143: }
144: }
|