hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Oleg Kalnichevski <ol...@apache.org>
Subject Re: Fw: SOCKS proxy example??
Date Fri, 23 Apr 2010 10:03:40 GMT
On Fri, 2010-04-23 at 09:52 +0200, John Smith wrote:
> So, I've used Google and done some attempt to write, but wothout any results.
> So, I realized that the best decision is
> set up environment before connection:
> 
> System.setProperty("socksProxySet", "true");
> System.setProperty("socksProxyHost", hostname);
> System.setProperty("socksProxyPort", Integer.toString(port));
> 
> and clear environment when you don't need to use SOCKS proxy:
> 
> System.out.println("Get down socks!");
> System.setProperty("socksProxySet", "false");
> System.setProperty("socksProxyHost", "");
> System.setProperty("socksProxyPort", "");
> 
> But it's useful for one thread tasks only.
> 
> So, what did you mean in that sentence:
> "Yes, you can use HttpParams to pass some custom configuration parameters
> to your custom SocketFactory and create a Socket instance bound to a
> SOCKS server."
> 
> I regret that I don't understand. How can it look in code?
> I think it's rhetorical question.
> 

---
public static void main(String[] args)throws Exception {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter("socks.host", "mysockshost");
    httpclient.getParams().setParameter("socks.port", 1234);
    httpclient.getConnectionManager().getSchemeRegistry().register(
            new Scheme("http", new MySchemeSocketFactory(), 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();
}

static class MySchemeSocketFactory implements SocketFactory {

    public Socket createSocket() throws IOException {
        return new Socket();
    }

    public Socket connectSocket(
            final Socket socket,
            final String host, int port,
            final InetAddress localAddress, int localPort,
            final HttpParams params)
                throws IOException, UnknownHostException,
ConnectTimeoutException {
        if (host == null) {
            throw new IllegalArgumentException("Host name may not be
null");
        }
        if (params == null) {
            throw new IllegalArgumentException("HTTP parameters may not
be null");
        }
        String proxyHost = (String) params.getParameter("socks.host");
        Integer proxyPort = (Integer) params.getParameter("socks.port");

        InetSocketAddress socksaddr = new InetSocketAddress(proxyHost,
proxyPort);
        Proxy proxy = new Proxy(Proxy.Type.SOCKS, socksaddr);
        Socket sock = new Socket(proxy);
        if (localAddress != null) {
            InetSocketAddress local = new
InetSocketAddress(localAddress, localPort);
            sock.bind(local);
        }
        int timeout = HttpConnectionParams.getConnectionTimeout(params);
        InetSocketAddress remote = new InetSocketAddress(host, port);
        try {
            sock.connect(remote, timeout);
        } catch (SocketTimeoutException ex) {
            throw new ConnectTimeoutException("Connect to " +
remote.getHostName() + "/"
                    + remote.getAddress() + " timed out");
        }
        return sock;
    }

    public boolean isSecure(final Socket sock) throws
IllegalArgumentException {
        return false;
    }

}
---

Oleg


---------------------------------------------------------------------
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