KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ProxyTunnelDemo


1 import java.io.BufferedReader JavaDoc;
2 import java.io.InputStreamReader JavaDoc;
3 import java.io.OutputStreamWriter JavaDoc;
4 import java.io.Writer JavaDoc;
5 import java.net.Socket JavaDoc;
6
7 import org.apache.commons.httpclient.ProxyClient;
8 import org.apache.commons.httpclient.UsernamePasswordCredentials;
9 import org.apache.commons.httpclient.auth.AuthScope;
10
11 /*
12  * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/examples/ProxyTunnelDemo.java,v 1.2 2004/06/12 22:47:23 olegk Exp $
13  * $Revision: 480424 $
14  * $Date: 2006-11-29 05:56:49 +0000 (Wed, 29 Nov 2006) $
15  * ====================================================================
16  *
17  * Licensed to the Apache Software Foundation (ASF) under one or more
18  * contributor license agreements. See the NOTICE file distributed with
19  * this work for additional information regarding copyright ownership.
20  * The ASF licenses this file to You under the Apache License, Version 2.0
21  * (the "License"); you may not use this file except in compliance with
22  * the License. You may obtain a copy of the License at
23  *
24  * http://www.apache.org/licenses/LICENSE-2.0
25  *
26  * Unless required by applicable law or agreed to in writing, software
27  * distributed under the License is distributed on an "AS IS" BASIS,
28  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29  * See the License for the specific language governing permissions and
30  * limitations under the License.
31  * ====================================================================
32  *
33  * This software consists of voluntary contributions made by many
34  * individuals on behalf of the Apache Software Foundation. For more
35  * information on the Apache Software Foundation, please see
36  * <http://www.apache.org/>.
37  *
38  * [Additional notices, if required by prior licensing conditions]
39  *
40  */

41
42 /**
43  * Example code for using {@link org.apache.commons.httpclient.ProxyClient}.
44  *
45  * @author Oleg Kalnichevski
46  * @author Michael Becke
47  */

48 public class ProxyTunnelDemo {
49
50     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
51         
52         ProxyClient proxyclient = new ProxyClient();
53         // set the host the proxy should create a connection to
54
//
55
// Note: By default port 80 will be used. Some proxies only allow conections
56
// to ports 443 and 8443. This is because the HTTP CONNECT method was intented
57
// to be used for tunneling HTTPS.
58
proxyclient.getHostConfiguration().setHost("www.yahoo.com");
59         // set the proxy host and port
60
proxyclient.getHostConfiguration().setProxy("10.0.1.1", 3128);
61         // set the proxy credentials, only necessary for authenticating proxies
62
proxyclient.getState().setProxyCredentials(
63             new AuthScope("10.0.1.1", 3128, null),
64             new UsernamePasswordCredentials("proxy", "proxy"));
65         
66         // create the socket
67
ProxyClient.ConnectResponse response = proxyclient.connect();
68         
69         if (response.getSocket() != null) {
70             Socket JavaDoc socket = response.getSocket();
71             try {
72                 // go ahead and do an HTTP GET using the socket
73
Writer JavaDoc out = new OutputStreamWriter JavaDoc(
74                     socket.getOutputStream(), "ISO-8859-1");
75                 out.write("GET http://www.yahoo.com/ HTTP/1.1\r\n");
76                 out.write("Host: www.yahoo.com\r\n");
77                 out.write("Agent: whatever\r\n");
78                 out.write("\r\n");
79                 out.flush();
80                 BufferedReader JavaDoc in = new BufferedReader JavaDoc(
81                     new InputStreamReader JavaDoc(socket.getInputStream(), "ISO-8859-1"));
82                 String JavaDoc line = null;
83                 while ((line = in.readLine()) != null) {
84                     System.out.println(line);
85                 }
86             } finally {
87                 // be sure to close the socket when we're done
88
socket.close();
89             }
90         } else {
91             // the proxy connect was not successful, check connect method for reasons why
92
System.out.println("Connect failed: " + response.getConnectMethod().getStatusLine());
93             System.out.println(response.getConnectMethod().getResponseBodyAsString());
94         }
95     }
96         
97 }
98
Popular Tags