KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > CustomHttpConnection


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/examples/Attic/CustomHttpConnection.java,v 1.3.2.1 2004/02/22 18:21:12 olegk Exp $
3  * $Revision: 1.3.2.1 $
4  * $Date: 2004/02/22 18:21:12 $
5  * ====================================================================
6  *
7  * Copyright 1999-2004 The Apache Software Foundation
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ====================================================================
21  *
22  * This software consists of voluntary contributions made by many
23  * individuals on behalf of the Apache Software Foundation. For more
24  * information on the Apache Software Foundation, please see
25  * <http://www.apache.org/>.
26  *
27  * [Additional notices, if required by prior licensing conditions]
28  *
29  */

30
31 import org.apache.commons.httpclient.HttpConnection;
32 import org.apache.commons.httpclient.HttpState;
33 import org.apache.commons.httpclient.HttpStatus;
34 import org.apache.commons.httpclient.HttpMethod;
35 import org.apache.commons.httpclient.URI;
36 import org.apache.commons.httpclient.UsernamePasswordCredentials;
37 import org.apache.commons.httpclient.protocol.Protocol;
38 import org.apache.commons.httpclient.methods.GetMethod;
39 import org.apache.commons.httpclient.ConnectMethod;
40
41 /**
42  * This example demonstrates how to establishing connection to an HTTP server
43  * when higher level of control is desired
44  *
45  * @author Armando Anton
46  * @author Oleg Kalnichevski
47  */

48
49 public class CustomHttpConnection
50 {
51     public static void main(String JavaDoc[] args) throws Exception JavaDoc
52     {
53         if (args.length != 1)
54         {
55             System.err.println("Usage: java CustomHttpConnection <url>");
56             System.err.println("<url> The url of a webpage");
57             System.exit(1);
58         }
59
60         URI uri = new URI(args[0].toCharArray()); // i like this constructor :)
61

62         String JavaDoc schema = uri.getScheme();
63         if ((schema == null) || (schema.equals("")))
64         {
65             schema = "http";
66         }
67         Protocol protocol = Protocol.getProtocol(schema);
68
69         HttpState state = new HttpState();
70
71         HttpMethod method = new GetMethod(uri.toString());
72         String JavaDoc host = uri.getHost();
73         int port = uri.getPort();
74
75         HttpConnection connection = new HttpConnection(host, port, protocol);
76
77         connection.setProxyHost(System.getProperty("http.proxyHost"));
78         connection.setProxyPort( Integer.parseInt(System.getProperty("http.proxyPort","80")));
79
80         if (System.getProperty("http.proxyUserName") != null)
81         {
82             state.setProxyCredentials(null, null,
83               new UsernamePasswordCredentials(
84                 System.getProperty("http.proxyUserName"),
85                 System.getProperty("http.proxyPassword")));
86         }
87
88         if (connection.isProxied() && connection.isSecure()) {
89             method = new ConnectMethod(method);
90         }
91         method.execute(state, connection);
92
93         if (method.getStatusCode() == HttpStatus.SC_OK) {
94             System.out.println(method.getResponseBodyAsString());
95         } else {
96             System.out.println("Unexpected failure: " + method.getStatusLine().toString());
97         }
98         method.releaseConnection();
99     }
100 }
101
Popular Tags