hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From John Smith <deaf....@gmail.com>
Subject Re: Fw: SOCKS proxy example??
Date Tue, 04 May 2010 13:07:10 GMT
Last example throw exception. I looked through PlainSocketFactory
source and mixed it with your example. See below:

public class Main {

     public static void main(String[] args) throws Exception {

        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, "UTF-8");
        HttpProtocolParams.setUseExpectContinue(params, false);
        HttpConnectionParams.setConnectionTimeout(params, 3000);

        DefaultHttpClient httpclient = new DefaultHttpClient(params);
        httpclient.getConnectionManager().getSchemeRegistry().register(
                new Scheme("http", new SocksSocketFactory("127.0.0.1",
9050), 80));

        HttpHost target = new HttpHost("www.apache.org", 80, "http");
        HttpGet req = new HttpGet("/");

        System.out.println("executing request to " + target + " via
SOCKS proxy");

        HttpResponse rsp = httpclient.execute(target, req);
        HttpEntity entity = rsp.getEntity();

        System.out.println("----------------------------------------");
        System.out.println(rsp.getStatusLine());
        Header[] headers = rsp.getAllHeaders();
        for (int i = 0; i < headers.length; i++) {
            System.out.println(headers[i]);
        }
        System.out.println("----------------------------------------");

        if (entity != null) {
            System.out.println(EntityUtils.toString(entity));
        }

        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        httpclient.getConnectionManager().shutdown();
    }

    public static class SocksSocketFactory implements SocketFactory {

        String proxyHost;
        int proxyPort;

        public SocksSocketFactory(String socksHost, int socksPort) {
            proxyHost = socksHost;
            proxyPort = socksPort;
        }

        public Socket createSocket() throws IOException {

            InetSocketAddress socksaddr = new InetSocketAddress(proxyHost,
                    proxyPort);
            Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
            Socket sock = new Socket(proxy);
            return sock;
        }

        public Socket connectSocket(Socket sock,
                String host,
                int port,
                InetAddress localAddress,
                int localPort,
                HttpParams params) throws IOException,
UnknownHostException, ConnectTimeoutException {

            if (host == null) {
                throw new IllegalArgumentException("Target host may
not be null.");
            }
            if (params == null) {
                throw new IllegalArgumentException("Parameters may not
be null.");
            }

            if (sock == null) {
                sock = createSocket();
            }

            if ((localAddress != null) || (localPort > 0)) {
                // we need to bind explicitly
                if (localPort < 0) {
                    localPort = 0; // indicates "any"
                }
                InetSocketAddress isa =
                        new InetSocketAddress(localAddress, localPort);
                sock.bind(isa);
            }

            int timeout = HttpConnectionParams.getConnectionTimeout(params);
            InetSocketAddress remoteAddress = new InetSocketAddress(host, port);

            try {
                sock.connect(remoteAddress, timeout);

            } catch (SocketTimeoutException ex) {
                throw new ConnectTimeoutException("Connect to " +
remoteAddress + " timed out");
            }
            return sock;
        }

        public boolean isSecure(Socket sock) throws IllegalArgumentException {
            if (sock == null) {
                throw new IllegalArgumentException("Socket may not be null.");
            }
            // This check is performed last since it calls a method implemented
            // by the argument object. getClass() is final in java.lang.Object.
            if (sock.isClosed()) {
                throw new IllegalArgumentException("Socket is closed.");
            }
            return false;
        }
    }
}

I hope it'll be useful.
Thank for your help!

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message