001: import java.io.BufferedReader;
002: import java.io.InputStreamReader;
003: import java.io.OutputStreamWriter;
004: import java.io.Writer;
005: import java.net.Socket;
006:
007: import org.apache.commons.httpclient.ProxyClient;
008: import org.apache.commons.httpclient.UsernamePasswordCredentials;
009: import org.apache.commons.httpclient.auth.AuthScope;
010:
011: /*
012: * $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 $
013: * $Revision: 480424 $
014: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
015: * ====================================================================
016: *
017: * Licensed to the Apache Software Foundation (ASF) under one or more
018: * contributor license agreements. See the NOTICE file distributed with
019: * this work for additional information regarding copyright ownership.
020: * The ASF licenses this file to You under the Apache License, Version 2.0
021: * (the "License"); you may not use this file except in compliance with
022: * the License. You may obtain a copy of the License at
023: *
024: * http://www.apache.org/licenses/LICENSE-2.0
025: *
026: * Unless required by applicable law or agreed to in writing, software
027: * distributed under the License is distributed on an "AS IS" BASIS,
028: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
029: * See the License for the specific language governing permissions and
030: * limitations under the License.
031: * ====================================================================
032: *
033: * This software consists of voluntary contributions made by many
034: * individuals on behalf of the Apache Software Foundation. For more
035: * information on the Apache Software Foundation, please see
036: * <http://www.apache.org/>.
037: *
038: * [Additional notices, if required by prior licensing conditions]
039: *
040: */
041:
042: /**
043: * Example code for using {@link org.apache.commons.httpclient.ProxyClient}.
044: *
045: * @author Oleg Kalnichevski
046: * @author Michael Becke
047: */
048: public class ProxyTunnelDemo {
049:
050: public static void main(String[] args) throws Exception {
051:
052: ProxyClient proxyclient = new ProxyClient();
053: // set the host the proxy should create a connection to
054: //
055: // Note: By default port 80 will be used. Some proxies only allow conections
056: // to ports 443 and 8443. This is because the HTTP CONNECT method was intented
057: // to be used for tunneling HTTPS.
058: proxyclient.getHostConfiguration().setHost("www.yahoo.com");
059: // set the proxy host and port
060: proxyclient.getHostConfiguration().setProxy("10.0.1.1", 3128);
061: // set the proxy credentials, only necessary for authenticating proxies
062: proxyclient.getState().setProxyCredentials(
063: new AuthScope("10.0.1.1", 3128, null),
064: new UsernamePasswordCredentials("proxy", "proxy"));
065:
066: // create the socket
067: ProxyClient.ConnectResponse response = proxyclient.connect();
068:
069: if (response.getSocket() != null) {
070: Socket socket = response.getSocket();
071: try {
072: // go ahead and do an HTTP GET using the socket
073: Writer out = new OutputStreamWriter(socket
074: .getOutputStream(), "ISO-8859-1");
075: out.write("GET http://www.yahoo.com/ HTTP/1.1\r\n");
076: out.write("Host: www.yahoo.com\r\n");
077: out.write("Agent: whatever\r\n");
078: out.write("\r\n");
079: out.flush();
080: BufferedReader in = new BufferedReader(
081: new InputStreamReader(socket.getInputStream(),
082: "ISO-8859-1"));
083: String line = null;
084: while ((line = in.readLine()) != null) {
085: System.out.println(line);
086: }
087: } finally {
088: // be sure to close the socket when we're done
089: socket.close();
090: }
091: } else {
092: // the proxy connect was not successful, check connect method for reasons why
093: System.out.println("Connect failed: "
094: + response.getConnectMethod().getStatusLine());
095: System.out.println(response.getConnectMethod()
096: .getResponseBodyAsString());
097: }
098: }
099:
100: }
|