1 import java.io.BufferedReader ; 2 import java.io.InputStreamReader ; 3 import java.io.OutputStreamWriter ; 4 import java.io.Writer ; 5 import java.net.Socket ; 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 41 42 48 public class ProxyTunnelDemo { 49 50 public static void main(String [] args) throws Exception { 51 52 ProxyClient proxyclient = new ProxyClient(); 53 proxyclient.getHostConfiguration().setHost("www.yahoo.com"); 59 proxyclient.getHostConfiguration().setProxy("10.0.1.1", 3128); 61 proxyclient.getState().setProxyCredentials( 63 new AuthScope("10.0.1.1", 3128, null), 64 new UsernamePasswordCredentials("proxy", "proxy")); 65 66 ProxyClient.ConnectResponse response = proxyclient.connect(); 68 69 if (response.getSocket() != null) { 70 Socket socket = response.getSocket(); 71 try { 72 Writer out = new OutputStreamWriter ( 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 in = new BufferedReader ( 81 new InputStreamReader (socket.getInputStream(), "ISO-8859-1")); 82 String line = null; 83 while ((line = in.readLine()) != null) { 84 System.out.println(line); 85 } 86 } finally { 87 socket.close(); 89 } 90 } else { 91 System.out.println("Connect failed: " + response.getConnectMethod().getStatusLine()); 93 System.out.println(response.getConnectMethod().getResponseBodyAsString()); 94 } 95 } 96 97 } 98 | Popular Tags |